How to save meta boxes post meta?

I create this function for metaboxes and to save information, but I can´t save the info and show it into the field.

My Code :

Read More
function reaction_buttons_meta2() 
{
    global $post;
    $reaction_buttons_off2 = false;
    if ( get_post_meta($post->ID, '_reaction_buttons_off2', true) ) {
        $reaction_buttons_off = true;
    }
    update_post_meta($post->ID, 'reaction_buttons_off2', $_POST['reaction_buttons_off2']);
    $meta=get_post_meta($post->ID, $field['reaction_buttons_off2'], true); 
    ?>
        <input type="text" id="reaction_buttons_off2" name="reaction_buttons_off2" value="<?php echo $meta[reaction_buttons_off2][0]; ?>">
    <?php
}

function reaction_buttons_meta_box2() 
{
    add_meta_box('reaction_buttons2','Reaction Buttons','reaction_buttons_meta2','post','side');
    add_meta_box('reaction_buttons2','Reaction Buttons','reaction_buttons_meta2','page','side');
}

add_action('admin_menu', 'reaction_buttons_meta_box2');

I need to put a simple field to store one option and after this show it in the template post.

Related posts

Leave a Reply

1 comment

  1. I have never created meta boxes myself, but looking at your code all you do is trying to update meta but you need to add it first with add_post_meta.

    Check this article on How To Create Custom Post Meta Boxes In WordPress.

    For your issue, pay attention to Saving the meta box data section. As you can see, their function uses add_post_meta, update_post_meta and delete_post_meta functions.