0

I know there are many questions about variants of this, but I have not been able to find an answer.

I have a metabox on the post type page, containing nothing but a checkbox. It seems like it won't save no matter what I do. Here is all the code for the metabox.

/*--------------------------------------------------------------------------*
 * Register metabox
/*--------------------------------------------------------------------------*/
function kasparabi_page_left_menu() {
    add_meta_box( 'kasparabi-left-menu-meta', __( 'Left Menu', 'kasparabi' ), 'kasparabi_render_left_menu_meta_box', 'page', 'side' );
}
add_action( 'add_meta_boxes', 'kasparabi_page_left_menu' );

/*--------------------------------------------------------------------------*
 * Callbacks
/*--------------------------------------------------------------------------*/
function kasparabi_render_left_menu_meta_box($post) {
    wp_nonce_field( basename( __FILE__ ), 'kasparabi-left-menu-meta_nonce' );

    ?>
        <p>
            <div>
                <label for="left-menu-checkbox">
                    <input type="checkbox" name="left-menu-checkbox" <?php (get_post_meta( $post->ID, 'left_menu_checkbox', true) == 'on') ? ' checked="checked"' : ''; ?> />
                    <?php _e( 'Display left menu', 'kasparabi' )?>
                </label>
            </div>
        </p>
    <?php
}

/*--------------------------------------------------------------------------*
 * Save functions
/*--------------------------------------------------------------------------*/
function kasparibi_left_menu_meta_save( $post_id, $post ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'kasparabi-left-menu-meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'kasparabi-left-menu-meta_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        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['left-menu-checkbox'] ) ? sanitize_html_class( $_POST['left-menu-checkbox'] ) : '' );

    $meta_key = 'left_menu_checkbox';
    $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 );
}
add_action( 'save_post', 'kasparibi_left_menu_meta_save' );
5
  • Have you checked in the database whether it is storing the data or not? Commented Dec 15, 2013 at 18:46
  • @Chittaranjan Yes, it is in the wp_postmeta table right? It is not there.
    – ptf
    Commented Dec 16, 2013 at 8:18
  • I followed s_ha_dum's answer and got it working. If you have unchecked the checkbox and saved the post, then you will not see an entry in the database. Please check the checkbox, then save. Now if you will check the postmeta table, you should see one entry for that post id with meta_key as left_menu_checkbox and value as on. Commented Dec 16, 2013 at 10:02
  • @Chittaranjan It worked? Could you post the code? Because I can't get it working. Is there any sort of plugin cache that needs refreshing?
    – ptf
    Commented Dec 16, 2013 at 10:10
  • @Chittaranjan I got it working now! Thanks for the help!
    – ptf
    Commented Dec 16, 2013 at 10:19

1 Answer 1

2

I notice a couple of things.

First, your callback uses two parameters but you don't ask for the second parameter when you hook it in. This:

add_action( 'save_post', 'kasparibi_left_menu_meta_save' );

Should be:

add_action( 'save_post', 'kasparibi_left_menu_meta_save' ,1 ,2 );

But, on the other hand, I don't see where you use that parameter at all. Maybe I missed it. You could probably just leave that second parameter out of the callback.

The real problem is here though:

<?php (get_post_meta( $post->ID, 'left_menu_checkbox', true) == 'on') ? ' checked="checked"' : ''; ?>

You don't echo the checked attribute.

<?php echo (get_post_meta( $post->ID, 'left_menu_checkbox', true) == 'on') ? ' checked="checked"' : ''; ?>

In other words, the value does save, but your code does not display the checked status correctly.

You could simplify that line with WordPress's checked function though.

<?php checked(get_post_meta( $post->ID, 'left_menu_checkbox', true),'on',true) ?>
0

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.