Why is my custom meta box input not saving

Try changing:

<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"/>

to this:

<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"/>

Reason: When the “name” attribute is set in the input field, that becomes the $_POST['input_name'] variable. Your custom meta name needs to use underscores so your input field’s name should as well.

Also you need to change this code:

$new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? sanitize_html_class( $_POST['rs_job_gallery'] ) : '' );

to something like this:

$new_meta_value = ( isset( $_POST['rs_job_gallery'] ) ? esc_attr( $_POST['rs_job_gallery'] ) : '' );

Since sanitize_html_class strips it down to numbers and letters so it can be used as a class in an element, but that’s not what you want.

Leave a Comment