Why is my custom meta box input not saving


I’ve added the following function to functions.php to add a custom meta box enabling the end user to input a gallery shortcode. I then intend to use this to place a gallery in a specific place on the page (away from the main the_content output). The function I’m using came from smashing magazine and, as you may have guessed, I have very little idea what I’m doing when it comes to php outwith the standard wordpress functions. (I do intend to learn PHP fully in the near future).

My meta box is showing up fine, but the data I enter is not saving. I.e. when I click ‘update’ on the post the meta box reverts back to my placeholder text.

// Load the post meta box setup function on the post editor screen.
add_action ( 'load-post.php', 'rs_post_meta_boxes_setup' );
add_action ( 'load-post-new.php', 'rs_post_meta_boxes_setup' );

//Meta box setup function
function rs_post_meta_boxes_setup() {

    //Add meta boxes to the 'add_meta_boxes' wordpress hook.
    add_action ( 'add_meta_boxes', 'rs_add_post_meta_boxes' );

    // Save post meta once created on the 'save_post' hook.
    add_action( 'save_post', 'rs_save_job_gallery_meta', 10, 2 );

// Create meta boxes for display on the post editor screen
function rs_add_post_meta_boxes() {

        'rs-job-gallery', //Meta box unique ID
        esc_html__( 'Job Gallery', 'rs-theme' ), //Title
        'rs_job_gallery_meta_box', //Callback Function
        'job', //Admin page on which to display meta box

// Display the post meta box
function rs_job_gallery_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'rs_job_gallery_nonce' ); ?>

        <label for="rs-job-gallery"> <?php _e( "Add the job gallery to the page. Use the following format: [galleryview id=x] where x is the ID of the job gallery.", 'redmansutherland' ); ?></label>
        <input class="widefat" type="text" placeholder="[galleryview id=X]" name="rs-job-gallery" id="rs-job-gallery" value="<?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?>" size="30"/>   

    <?php }

// Save the meta box's post metadata.
function rs_save_job_gallery_meta( $post_id, $post ) {

    // Verify the nonce before proceeding.
    if ( !isset( $_POST['rs_job_gallery_nonce'] ) || !wp_verify_nonce( $_POST['rs_job_gallery_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    // Get the post type object.
    $post_type = get_post_type_object( $post->post_type );

    // Check if the current user has permission to edit the post.
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    // Get the posted data and sanitize it for use as an HTML class.
    $new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? sanitize_html_class( $_POST['rs_job_gallery'] ) : '' );

    // Get the meta key.
    $meta_key = 'rs_job_gallery';

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


Thanks in advance for any insights anyone can provide, your help is much appreciated!

, , , Raskolnik 4 years 2020-02-13T08:39:17-05:00 0 Answers 129 views 0

Leave an answer