0

I'm putting together a fairly simple Custom Page for a friend on their WordPress site, but I'm having some real difficulties getting the contents of the Metaboxes to save correctly. WordPress doesn't seem to behave like I'd expect PHP to, so when I place echo statements around the place to see where it got up to, they don't always appear, even when the code must be running. Very strange.

Can anyone help point out what I've done wrong? (Sorry for the large amount of code!)

function metabox_exhibition_setup() {
    /* Add 'start_date' hook. */
    add_meta_box('gc_start_date', 
            esc_html__('Exhibition Start Date'),
            'create_metabox_exhibition_start_date',
            'gc_exhibition', 
            'side', 
            'default');

    /* Add 'finish_date' hook. */
    add_meta_box('gc_finish_date', 
            esc_html__('Exhibition Finish Date'),
            'create_metabox_exhibition_finish_date',
            'gc_exhibition', 
            'side', 
            'default');

    /* Add 'subtitle' hook. */
    add_meta_box('gc_subtitle', 
            esc_html__('Exhibition Subtitle'),
            'create_metabox_exhibition_subtitle',
            'gc_exhibition', 
            'normal', 
            'core');

    add_action( 'save_post', 'save_metabox_exhibition', 10, 2 );        
}

/* Save the meta box's post metadata. */
function save_metabox_exhibition( $post_id, $post ) {
    $fields = 0;

    if($_POST) {
    $metaboxes[0]['nonce'] = $_POST['exhibition_finish_date_nonce'];
    $metaboxes[0]['data'] = $_POST['gc_finish_date'];
    $metaboxes[1]['nonce'] = $_POST['exhibition_start_date_nonce'];
    $metaboxes[1]['data'] = $_POST['gc_start_date'];
    $metaboxes[2]['nonce'] = $_POST['exhibition_subtitle_nonce'];
    $metaboxes[2]['data'] = $_POST['gc_subtitle'];
    $fields = count($metaboxes);
        $fields = count($metaboxes);
    }

    //print_r($metaboxes);

    for($i = 0; $i <= $fields ; $i++ ) {
        /* Verify the nonce before proceeding. */
        if ( !isset( $metaboxes[$i]['nonce'] ) || !wp_verify_nonce( $metaboxes[$i]['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 */
        $new_meta_value = ( isset( $metaboxes[$i]['data'] ) ? sanitize_html_class( $metaboxes[$i]['data'] ) : '' );

        /* Get the meta key. */
        $meta_key = $metaboxes[$i]['data'];

        /* 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 );
    }
5
  • What do you mean, custom form? Do you mean a custom post type? Or custom fields added to a pre-exisitng post type?
    – vancoder
    Commented Apr 4, 2013 at 20:44
  • You have to die to see inside save_post, meaning: echo $var; wp_die();. Or use FirePHP or other debug method.
    – brasofilo
    Commented Apr 4, 2013 at 20:50
  • @vancoder Sorry, I fixed my terminology. Commented Apr 4, 2013 at 20:51
  • @brasofilo Thanks, that worked! It seems the $metaboxes array IS getting all the data correctly after all. Hmm. So what's wrong? Commented Apr 4, 2013 at 20:51
  • Makes a mental note: Ah, so all the nonces are the same. They're not unique to the input, just the page. That's good to know. Commented Apr 4, 2013 at 20:55

1 Answer 1

2

Aha! Stupid error. I didn't give it a proper METAKEY!

This seems to have fixed things:

/* Save the meta box's post metadata. */
function save_metabox_exhibition( $post_id, $post ) {
$fields = 0;

if($_POST) {
    $metaboxes[0]['nonce'] = $_POST['exhibition_finish_date_nonce'];
    $metaboxes[0]['data'] = $_POST['gc_finish_date'];
    $metaboxes[0]['meta'] = 'gc_finish_date';
    $metaboxes[1]['nonce'] = $_POST['exhibition_start_date_nonce'];
    $metaboxes[1]['data'] = $_POST['gc_start_date'];
    $metaboxes[1]['meta'] = 'gc_start_date';
    $metaboxes[2]['nonce'] = $_POST['exhibition_subtitle_nonce'];
    $metaboxes[2]['data'] = $_POST['gc_subtitle'];
    $metaboxes[2]['meta'] = 'gc_subtitle';
    $fields = count($metaboxes);
}


for($i = 0; $i <= $fields ; $i++ ) {
    /* Verify the nonce before proceeding. */
    if ( !isset( $metaboxes[$i]['nonce'] ) || !wp_verify_nonce( $metaboxes[$i]['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 */
    $new_meta_value = ( isset( $metaboxes[$i]['data'] ) ? sanitize_text_field(    $metaboxes[$i]['data'] ) : '' );

    /* Get the meta key. */
    $meta_key = $metaboxes[$i]['meta'];

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

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.