WP 如何在元字段中保存逗号格式数字
WP How can i save a comma format number in a meta field
我已将自定义字段添加到 post 编辑器以将温度保存为数值。
如果我在此字段中输入例如“15,2”或“15.2”并保存 post,它会保存为“152”。
我该如何解决?
表单字段:
<label for="temp_min">Temp Min.:</label><br />
<input class="widefat" type="text" name="temp_min" id="temp_min" value="<?php echo esc_attr( get_post_meta( $object->ID, '_temp_min', true ) ); ?>" size="30" />
保存 class:
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST[$fields[$i]] ) ? sanitize_html_class( $_POST[$fields[$i]] ) : '' );
/* Get the meta key. */
$meta_key = $meta_keys[$i];
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
sanitize_html_class
删除 .
或 ,
所以 19,5 或 19.5 将得到 195.
Strips the string down to A-Z,a-z,0-9,_,-.
WP 抄本:https://codex.wordpress.org/Function_Reference/sanitize_html_class
只需使用 sanitize_text_field
或 (float) $_POST[$fields[$i]]
:
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST[$fields[$i]] ) ? sanitize_text_field( $_POST[$fields[$i]] ) : '' );
我已将自定义字段添加到 post 编辑器以将温度保存为数值。 如果我在此字段中输入例如“15,2”或“15.2”并保存 post,它会保存为“152”。
我该如何解决?
表单字段:
<label for="temp_min">Temp Min.:</label><br />
<input class="widefat" type="text" name="temp_min" id="temp_min" value="<?php echo esc_attr( get_post_meta( $object->ID, '_temp_min', true ) ); ?>" size="30" />
保存 class:
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST[$fields[$i]] ) ? sanitize_html_class( $_POST[$fields[$i]] ) : '' );
/* Get the meta key. */
$meta_key = $meta_keys[$i];
/* Get the meta value of the custom field key. */
$meta_value = get_post_meta( $post_id, $meta_key, true );
/* If a new meta value was added and there was no previous value, add it. */
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
/* If the new meta value does not match the old value, update it. */
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
/* If there is no new meta value but an old value exists, delete it. */
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
}
sanitize_html_class
删除 .
或 ,
所以 19,5 或 19.5 将得到 195.
Strips the string down to A-Z,a-z,0-9,_,-.
WP 抄本:https://codex.wordpress.org/Function_Reference/sanitize_html_class
只需使用 sanitize_text_field
或 (float) $_POST[$fields[$i]]
:
/* Get the posted data and sanitize it for use as an HTML class. */
$new_meta_value = ( isset( $_POST[$fields[$i]] ) ? sanitize_text_field( $_POST[$fields[$i]] ) : '' );