0

我在帖子编辑器中添加了一个自定义字段,以将温度保存为数值。如果我在此字段中输入例如“15,2”或“15.2”并保存帖子,则将其保存为“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" /> 

保存类:

      /* 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 );
  }  
4

1 回答 1

2

sanitize_html_class删除.,19,5 或 19.5 将导致 195。

将字符串剥离到 AZ,az,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]] ) : '' );
于 2017-10-15T16:45:39.500 回答