WordPress: Saving post metadata entry in the admin post panel

I’m working on a plugin, where I have defined a hook which is supposed to create a custom post field in the admin panel. The code for the same:

    //ADDING CUSTOM FIELDS IN ADMIN PANEL
add_action('add_meta_boxes', 'jericho_meta');
add_action('save_post', 'jericho_saved');

function jericho_meta()
{
    add_meta_box('jericho_name', 'Favorite PPV', 'jericho_handler', 'post');
}

function jericho_handler()
{   
    $value = get_post_custom($post->ID);
    $namey = esc_attr($value['jericho_name'][0]);
    echo '<label for = "jericho_name">Favorite PPV</label><input type = "text" id = "jericho_name" name = "jericho_name" value = "'.$namey.'" />';

}

function jericho_saved()
{
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    {
        return;
    }

    if(!current_user_can('edit_post'))
    {
        return;
    }

    if(isset($_POST['jericho_name']))
    {
        update_post_meta($post_id, 'jericho_name', $_POST['jericho_name']);
    }
}

This code generates a custom post field in the admin post panel as shown in the screenshot below:

Read More

enter image description here

However, when I enter a value in that text field and click on Update, the input value never gets saved inside the text field when I try checking the field on refreshing the page.

What seems to be wrong with my code?

EDIT 1:

I have updated my code and added a new action named save_post and defined its corresponding function. However, the problem seems to be with the way I have defined the input field itself, because when I tried inspecting the text field’s element, this is what I got:

<input type="text" id="jericho_name" name="jericho_name" value>

Related posts

1 comment

  1. Based on the code you provided i can see that you are not registering callback/trigger for saving post meta data.

    You need to handle this by yourself (will not be handled automaticly).
    Currently, what you did is you tied “jericho_handler()” function to render data when post edit page renders. And that works just fine, as it should.

    You need to add additional function which will be triggered on ‘save_post’ where you will handle saving data into the database.

    add_action( 'save_post', 'cd_meta_box_save' );
    function cd_meta_box_save( $post_id )
    {
        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    
        // if our nonce isn't there, or we can't verify it, bail
        if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    
        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post' ) ) return;
    
        // now we can actually save the data
        $allowed = array( 
            'a' => array( // on allow a tags
                'href' => array() // and those anchors can only have href attribute
            )
        );
    
        // Make sure your data is set before trying to save it
        if( isset( $_POST['my_meta_box_text'] ) )
            update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
    
        if( isset( $_POST['my_meta_box_select'] ) )
            update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
    
        // This is purely my personal preference for saving check-boxes
        $chk = isset( $_POST['my_meta_box_check'] ) && $_POST['my_meta_box_select'] ? 'on' : 'off';
        update_post_meta( $post_id, 'my_meta_box_check', $chk );
    }
    ?>
    

    Please check detail tutorial over here. Code i pasted above is from this link.

    If you are already registering this handler and you are still encountering issues, please update your question with other parts of code as well.

Comments are closed.