• I’ve added a custom check-box which displays all the time in wordpress post editor on the side, like following:

    <label for="pp_is_feat_meta">
    <input type="checkbox" name="pp_is_feat_meta" id="pp_is_feat_meta" value="" /> Is the post featured?</label>

    problem 1: When i check the checkbox, click the Update button in post editor, it gets unchecked again after the page loads. How do i keep it checked?

    problem 2: How to check if the checkbox is checked in a particular page (like index.php). For example, in index.php, for all the posts check the following and echo if checked :

    if(checkbox is checked) {
     echo "This post's title is BLABLA and checkbox is checked";
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • sounds like you are not ‘saving’ meta fields after you click update…

    You need to do that by creating a function and doing an action for it

    add_action( 'save_post', 'my_function_save_custom_edit_stuff', 10, 2 );

    Here are some things to look up:
    add_post_meta()
    delete_post_meta()
    update_post_meta()

    Example would be:

    add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    To use that meta field you would do something like this:

    $is_featured = get_user_meta($post->ID, 'pp_is_feat_whatever', true);

    if ($is_featured) etc.

    Thread Starter prempwp

    (@prempwp)

    @frumph: Thanks for your reply, much appreciated.

    This is the callback function where i create the check-box:

    function pp_is_feat_cb(){
    global $post;
    $meta = get_post_meta($post->ID, 'pp_is_feat_meta', true);
    
    wp_nonce_field(__FILE__, 'pp_nonce');
    ?>
    <label for="pp_is_feat_meta">
    <input type="checkbox" name="pp_is_feat_meta" id="pp_is_feat_meta" value="" />
    Is the post featured?</label>
    
    <?php
    }

    And heres where i save the meta_boxes:

    add_action('save_post',function(){
    global $post;
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    if($_POST && wp_verify_nonce($_POST['pp_nonce'], __FILE__)) {
    	if(isset($_POST['pp_is_feat_meta'])){
    		update_post_meta($post->ID, 'pp_is_feat_meta', $_POST['pp_is_feat_meta']);
    	}
    }
    });

    Am i wrong somewhere?

    Meh, not really. I would have done it a bit different

    however, check this out, you’re not putting the “CHECKED” back into your input checkbox which is why it’s not showing again

    check this function out:

    http://codex.wordpress.org/Function_Reference/checked

    example:

    <?php checked( $meta, 1 ); ?>

    Thread Starter prempwp

    (@prempwp)

    <input type="checkbox" name="pp_is_feat_meta" id="pp_is_feat_meta" value="1" <?php checked( 'pp_is_feat_meta' , 1 ); ?> />

    so, is the above statement alright?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Checking if checkbox is checked’ is closed to new replies.