Metabox not saving values
Question
I have a problem. My fields are showing in the meta box, but saving does not work:
// Create the metabox and fields
function food_meta_box_markup($object) {
wp_nonce_field(basename(__FILE__), "meta-box-nonce");
?>
<div>
<label for="np-calories" class="np-label"><?php _e( 'Energy (kcal)', 'nutriplus' ) ?></label>
<input name="np_calories" type="number" class="np-field" value="<?php echo get_post_meta($object->ID, "np_calories", true); ?>">
</div>
<?php
}
function add_food_meta_box() {
add_meta_box("food-meta-box", __( 'Nutritional Information', 'nutriplus' ), "food_meta_box_markup", "np-food", "side", "high", null);
}
add_action("add_meta_boxes", "add_food_meta_box");
// Save the metabox and fields
function food_save_meta_box_data( $post_id ){
// verify taxonomies meta box nonce
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
if( $post->post_type == "np-food" ) {
// store custom fields values
// energy kcal string
if ( isset( $_REQUEST['np_calories'] ) ) {
update_post_meta( $post_id, 'np_calories', sanitize_text_field( $_POST['np_calories'] ) );
}
}
}
add_action( 'save_post_food', 'food_save_meta_box_data' );
?>
My Metabox values don´t save 🙁
Regards
Buddy
0
metabox
4 years
2020-02-13T08:38:54-05:00
2020-02-13T08:38:54-05:00 0 Answers
62 views
0
Leave an answer