0

I created this meta box for my custom theme. I thought I followed all the guidelines, but I'm hitting a wall. The meta box and all fields are showing, but when I update, the data is not saved.

Meta box addition:

function drw_events_metabox(){
    add_meta_box('events_info', 'EVENT INFO', 'drw_evenet_info_clbk', 'drw-events', 'side', 'high');
}

function drw_evenet_info_clbk($post){
    wp_nonce_field('drw_event_info', 'drw_event_info_nonce');
    $datevalue = get_post_meta( $post->ID, '_event_date_valkey', true);
    $starttimevalue = get_post_meta( $post->ID, '_event_time_valkey', true);
    $endtimevalue = get_post_meta( $post->ID, '_event_endtime_valkey', true);
    $covervalue = get_post_meta( $post->ID, '_event_cover_valkey', true);
    $freeEvent = get_post_meta( $post->ID, '_event_free_valkey', true);
    $stdTicketLink = get_post_meta( $post->ID, '_event_stdticket_valkey', true);
    $customTicketLink = get_post_meta( $post->ID, '_event_customTicketLink_valkey', true);
    $eventCancelled = get_post_meta( $post->ID, '_event_cancel_valkey', true);


    //event date
    echo'<br><label for="drw_event_date_field">Event Date </label>';
    echo '<input type="date" id="drw_event_date_field" name="drw_event_date_field" value="'.esc_attr($datevalue).'" required/><br><br><hr><br>';

    //event time
    echo'<label for="drw_event_time_field">Event start time </label>';
    echo '<input type="time" id="drw_event_time_field" name="drw_event_time_field" value="'.esc_attr($starttimevalue).'" required/><br><br>';


    echo'<label for="drw_event_endtime_field">Event end time </label>';
    echo '<input type="time" id="drw_event_endtime_field" name="drw_event_endtime_field" value="'.esc_attr($endtimevalue).'" required/><br><br><hr><br>';

    //event price
    echo'<label for="drw_event_free_field">Event is free </label>';
    echo'<input type="checkbox" id="drw_event_free_field" name="drw_event_free_field" value="'.esc_attr($freeEvent).'" checked/><br><br>';

    echo'<label for="drw_event_cover_field">Event cover $ </label>';
    echo '<input type="number" id="drw_event_cover_field" name="drw_event_cover_field" value="'.esc_attr($covervalue).'" size="2"/><br><br><hr><br>';


    //event ticket
    echo '<h2><strong><em>Choose a ticket purchase link</em></strong></h2><br>';
    echo'<label for="drw_event_stdtktlink_field">Default link </label>';
    echo '<input type="checkbox" id="drw_event_stdtktlink_field" name="drw_stdtktlink_field" value="#" checked/><br><br>';
    echo'<label for="drw_event_customTicketLink_field">Custom link </label>';
    echo '<input type="text" id="drw_event_customTicketLink_field" name="drw_customTicketLink_field" value="'.esc_attr($customTicketLink).'"/><br><br><hr><br>';

    //event cancellation
    echo '<h2><strong><em>**Use only for event cancellation!</em></strong></h2><br>';
    echo'<label for="drw_event_cancelled_field">Event Cancelled </label>';
    echo '<input type="checkbox" id="drw_event_cancelled_field" name="drw_event_cancelled_field" value="'.esc_attr($eventCancelled).'"/><br><br><hr><br>';

}

add_action('add_meta_boxes', 'drw_events_metabox');

Saving meta box data:

function save_drw_events_metabox_data( $post_id ) {

    if( ! isset( $_POST['drw_event_info_nonce'] ) ){
        return;
    }

    if( ! wp_verify_nonce( $_POST['drw_event_info_nonce'], 'save_drw_events_metabox_data') ) {
        return;
    }

    if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        return;
    }

    if( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }


    $event_date = sanitize_text_field( $_POST['drw_event_date_field'] );
    $event_time = sanitize_text_field( $_POST['drw_event_time_field'] );
    $event_entime = sanitize_text_field( $_POST['drw_event_endtime_field'] );
    $event_free = sanitize_text_field( $_POST['drw_event_free_field'] );
    $event_cover = sanitize_text_field( $_POST['drw_event_cover_field'] );
    $event_linkdef = sanitize_text_field( $_POST['drw_stdtktlink_field'] );
    $event_tix = sanitize_text_field( $_POST['drw_customTicketLink_field'] );
    $event_cancel = sanitize_text_field( $_POST['drw_event_cancelled_field'] );


    if( ! isset( $_POST['drw_event_date_field'] ) ) {
        return;  
        update_post_meta($post_id, '_event_date_valkey', $event_date);
    }

    if( ! isset( $_POST['drw_event_time_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_time_valkey', $event_time);
    }

    if( ! isset( $_POST['drw_event_endtime_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_endtime_valkey', $event_entime);
    }

    if( ! isset( $_POST['drw_event_cover_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_cover_valkey', $event_cover);
    }


    if( ! isset( $_POST['drw_event_free_field'] ) ) {
        return;   
        update_post_meta($post_id, '_event_free_valkey', true);
    } else {
        update_post_meta( $post_id, '_event_free_valkey', FALSE );
    }


    if( ! isset( $_POST['drw_stdtktlink_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_stdticket_valkey', $event_linkdef);
    }

    if( ! isset( $_POST['drw_customTicketLink_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_customTicketLink_valkey', $event_tix);
    }


    if( ! isset( $_POST['drw_event_cancelled_field'] ) ) {
        return;
        update_post_meta($post_id, '_event_cancel_valkey', true);
    } else {
        update_post_meta( $post_id, '_event_cancel_valkey', FALSE );
    }

}

add_action( 'save_post', 'save_drw_events_metabox_data');

Really I have no idea what I am missing at this point. All of my research is saying that the CODE is right, but still no data is saved!

Please help.

  • 1
    there is a lot of return and everything after the return is not executed – mmm Apr 1 '17 at 20:40
0

To follow-up on the comment made by @mmm, you've got a number of statements like:

if ( ! isset ($_POST['drw_event_date_field'] ) ) {
    return ;
update_post_meta ($post_id, '_event_date_valkey', $event_date) ;
}

Notice that you are wrapping your calls to update_post_meta() inside the !isset() conditionals.

What you want is:

if ( ! isset ($_POST['drw_event_date_field'] ) ) {
    return ;
}
update_post_meta ($post_id, '_event_date_valkey', $event_date) ;

Also, even when you correct the logic as above, you might still fail to save some values, e.g., if "drw_event_date_field" is not set you will not save any values, even those that are set...which is probably not what you want.

You'd probably be better off with logic such as:

$fields = array (
    '_event_time_valkey' => 'drw_event_date_field',
    '_event_endtime_valkey' => 'drw_event_time_field',
    '_event_endtime_valkey' => 'drw_event_endtime_field',
    '_event_free_valkey' => 'drw_event_free_field',
    '_event_cover_valkey' => 'drw_event_cover_field',
    '_event_stdticket_valkey' => 'drw_stdtktlink_field',
    '_event_customTicketLink_valkey' => 'drw_customTicketLink_field',
    '_event_cancel_valkey' => 'drw_event_cancelled_field',
    ) ;

foreach ($fields as $meta_key => $field) {
    if (!isset ($_POST[$field])) {
        if ($field == 'drw_event_cancelled_field') {
            update_post_meta ($post_id, $meta_key, false) ;
            }

        continue ;
        }

    $meta_val = sanitize_text_field ($_POST[$field]) ;
    update_post_meta ($post_id, $meta_key, $meta_val) ;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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