WP如何在元字段中保存逗号格式编号

时间:2017-10-15 16:14:51

标签: wordpress post-meta

我在帖子编辑器中添加了一个自定义字段,将温度保存为数字值。 如果我举个例子" 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 );
  }  

1 个答案:

答案 0 :(得分:2)

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

  

将字符串向下拉到A-Z,a-z,0-9,_, - 。

WP codex: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]] ) : '' );