WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create my own custom fields meta box filled with just checkboxes in pages. I'm basing it off this tutorial here. I was able to get the original tutorial to work, but when I started to simplify it down just to saving a checkbox, it stopped working: the checkbox won't save the 'on' state when I update the page.

I'm sure this has something to do with the saving portion of the function. Anyone have any clues, tips, or hints? Thanks!

<?php function cd_meta_box_cb() { 
// Some settings first:
    global $post;
    $check = isset( $values['page_title_off'] ) ? esc_attr( $values['page_title_off'][0] ) : '';    
    // We'll use this nonce field later on when saving.
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
// Render the custom fields:
        <input type="checkbox" id="page_title_off" name="page_title_off" <?php checked( $check, 'on' ); ?> />
        <label for="page_title_off">Turn off page title</label>
<?php } ?>
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;
    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchors can only have href attribute
    // Save
    if( isset( $_POST['page_title_off'] ) )
    update_post_meta( $post_id, 'page_title_off', $_POST['page_title_off'] );

share|improve this question

I went through all this yesterday!

According to the codex there must be a post id given to current_user_can to check 'edit_post', so your capability check should be:

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

Also, your final two lines should be more like this:

$chk = isset( $_REQUEST['page_title_off'] ) ? 'on' : 'off';  
update_post_meta( $post_id, 'page_title_off', $chk );  

This should turn the post meta on or off depending on whether the checkbox was ticked.

share|improve this answer
Ah, that makes sense! However, it's still not saving the check every update click. Do I have this right? jsfiddle.net/foomarks/mH2QT – Marc P Mar 26 '13 at 3:28
Change your $_POST to $_REQUEST: '$chk = isset( $_REQUEST['page_title_off'] ) ? 'on' : 'off';' – Bev Mar 26 '13 at 11:54
Thanks for that clue! But argh, still not working. I feel like I'm so close! I updated the code to your suggestion: jsfiddle.net/foomarks/mH2QT/1 – Marc P Mar 26 '13 at 15:33
I would suggest adding some debug code to see if the save function is actually being called, and if so, what the values of all the variables are. – Bev Mar 26 '13 at 19:55
Will do! Thanks for the helpful headstart! – Marc P Mar 27 '13 at 5:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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