3

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() {

    add_meta_box(
        '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
        'normal',
        'default'
        );
}

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

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

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

    <?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!

1 Answer 1

4

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.

9
  • Hi Jared, that worked great, thanks! Unfortunately the meta box is stripping the square brackets and equals sign from the shortcode I enter. I have a rough idea this has something to do with security (stopping people entering scripts??)?
    – Raskolnik
    Commented Jan 25, 2012 at 13:36
  • Also, I assume if I want to add further meta boxes I have to duplicate the above code for each one?
    – Raskolnik
    Commented Jan 25, 2012 at 15:38
  • @Raskolnik When you save the meta, try escaping it with esc_attr() before its saved, and then it should be alright. If my answer was good enough for you please accept it. :)
    – Jared
    Commented Jan 25, 2012 at 17:50
  • I've tried doing this, but with my (very) limited knowledge of PHP I'm not entirely sure I'm putting it in the right place. I've tried it everywhere I thought you meant but with no luck. Could you show me where this should go. I also wondered if sanitize_html_class had anything to do with it as I've read that this removes spaces so the content can be used as a class. Sorry to keep bothering you with further questions; the next thing on my agenda is to learn PHP properly!! Should I accept your answer if I have further questions? Not sure of the protocol... :S
    – Raskolnik
    Commented Jan 26, 2012 at 0:36
  • Don't worry, WPSE is all about questions! Also you want to be sure your problem is resolved before accepting an answer. One thing you can do is take this code: <?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?> that displays the meta and change it to this: [<?php echo esc_attr( get_post_meta( $object->ID, 'rs_job_gallery', true ) ); ?>] -- Just put brackets around it manually when outputting the meta data.
    – Jared
    Commented Jan 26, 2012 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.