2017-10-15 1 views
0

J'ai ajouté un champ personnalisé à l'éditeur de publication pour enregistrer une température en tant que valeur numérique. Si je mets par exemple "15,2" ou "15,2" dans ce champ et sauvegardez le post, il sera enregistré sous "152".WP Comment puis-je enregistrer un numéro de format virgule dans un champ meta

Comment puis-je résoudre ce problème?

champ de formulaire:

<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" /> 

sauver classe:

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

Répondre

2

sanitize_html_class enlever . ou si , ou 19,5 19,5 se traduira par 195.

Strips la chaîne vers le bas à AZ, az, 0-9, _, -.

codex WP: https://codex.wordpress.org/Function_Reference/sanitize_html_class

Il suffit d'utiliser sanitize_text_field ou (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]]) : '');