wordpressmetadatacustom-post-typesaving-data

How to save Meta Data in WordPress back-end with PHP


I am relatively new to Post Meta Data in the WordPress backend using PHP. I have written the code that creates the Meta Data. I need help saving the data for which I have written. It will also need to allow me to edit the data once saved.

In this case its for a text field.

I have created the Meta Data for the input field which displays well in the back-end WordPress admin area.

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
    add_meta_box( 'my-meta-box-id', 'Job Title', 'cd_meta_box_cb', 'people', 'normal', 'high' );
    /* Save post meta on the 'save_post' hook. */
    add_action( 'save_post', 'cd_meta_box_add', 10, 2 );
}

function cd_meta_box_cb()  
{  
    echo "<input type='text' name='jobtitle'>";     
} 

I just need assistance with the code that will save the above Meta Data to the DB and allow for editing and revisions


Solution

  • You were almost there.

    The final part of the puzzle is the function that saves the metadata, but first we need to make a few adjustments to your existing code:

    1. add_action( 'save_post', 'cd_meta_box_add', 10, 2 ); has to be moved outside cd_meta_box_add(), and

    2. Change add_action( 'save_post', 'cd_meta_box_add', 10, 2 ); into add_action( 'save_post', 'cd_meta_box_add' ); as this action hook only receives one parameter (the post ID), and

    3. You need to define the function that will process the data (and it can't be cd_meta_box_add as you have it now so we'll create a new one called save_cd_meta_box_data).

    /* Save post meta on the 'save_post' hook. */
    add_action( 'save_post', 'save_cd_meta_box_data' );
    
    function save_cd_meta_box_data( $post_id ) {
    
        // Autosaving, bail.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
    
        // @TODO
        // You should add some additional security checks here
        // eg. nonce, user capabilities, etc, to prevent
        // malicious users from doing bad stuff.
    
        /* OK, it's safe for us to save the data now. */
    
        // Make sure that it is set.
        if ( ! isset( $_POST['jobtitle'] ) ) {
            return;
        }
    
        // Sanitize user input.
        $my_data = sanitize_text_field( $_POST['jobtitle'] );
    
        // Update the meta field in the database.
        update_post_meta( $post_id, '_job_title', $my_data );
    }
    
    1. Now that we're successfully saving the metadata into the database, let's allow the user to view it / edit it:
    function cd_meta_box_cb( $post )  
    {  
        $job_title = get_post_meta( $post->ID, '_job_title', true );
        echo "<input type='text' name='jobtitle' value='" . esc_attr( $job_title ) . "'>";     
    }
    

    The final code should look like this:

    /* Register and display metabox */
    add_action( 'add_meta_boxes', 'cd_meta_box_add' );
    
    function cd_meta_box_add()
    {
        add_meta_box( 'my-meta-box-id', 'Job Title', 'cd_meta_box_cb', 'people', 'normal', 'high' );
    }
    
    function cd_meta_box_cb( $post )  
    {  
        $job_title = get_post_meta( $post->ID, '_job_title', true );
        echo "<input type='text' name='jobtitle' value='" . esc_attr( $job_title ) . "'>";     
    }
    
    /* Save post meta on the 'save_post' hook. */
    add_action( 'save_post', 'save_cd_meta_box_data' );
    
    function save_cd_meta_box_data( $post_id ) {
    
        // Autosaving, bail.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        }
    
        // @TODO
        // You should add some additional security checks here
        // eg. nonce, user capabilities, etc, to prevent
        // malicious users from doing bad stuff.
    
        /* OK, it's safe for us to save the data now. */
    
        // Make sure that it is set.
        if ( ! isset( $_POST['jobtitle'] ) ) {
            return;
        }
    
        // Sanitize user input.
        $my_data = sanitize_text_field( $_POST['jobtitle'] );
    
        // Update the meta field in the database.
        update_post_meta( $post_id, '_job_title', $my_data );
    }