• I have a custom made checkbox in wordpress post editor.

    When i check the checkbox and click the update button on the post editor, it goes back to being unchecked state.

    I figured, that the problem is caused because somehow, my input statement’s checked() part below doesn’t echo ‘checked=”checked”‘ when it is checked.

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

    Please help me solve this.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try it with:

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

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

    Thread Starter prempwp

    (@prempwp)

    Hey, thanks, and nope, still the same… I guess that false at the end just makes sure that the checked=”checked” does not get displayed in browser.

    I am actually concerned if get_option(‘pp_is_feat_meta’) part is correct

    Moderator keesiemeijer

    (@keesiemeijer)

    For testing purposes try echoing the pp_is_feat_meta option and see what it returns:

    echo 'pp_is_feat_meta = ' . get_option('pp_is_feat_meta');

    How do you save the forms options?

    Thread Starter prempwp

    (@prempwp)

    Hey keesiemeijer, thanks again.

    I tried echoing that, but all it displays is:

    pp_is_feat_meta =

    Which means, there is nothing in get_option(‘pp_is_feat_meta’); which is why the problem is i suppose.

    I also tried manually adding checked=”checked” within the input, like so:

    echo "<input type='checkbox' name='pp_is_feat_meta' id='pp_is_feat_meta' value='1'
    checked='checked' />";

    And here, the check-box remains checked, which means the only issue is to find out how to get the check-box state (in our case we are using get_option(‘pp_is_feat_meta’);, which doesn’t seem to work)

    As for saving the form options:

    add_action('save_post',function($post_id){
    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']);
    	}
    }

    this one here, saves it on update.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    add_action('save_post','save_pp_is_feat_meta');
    function save_pp_is_feat_meta($post_id){
      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']);
      	}
      }
    }

    Can you paste and submit the full code of your metabox code into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Thread Starter prempwp

    (@prempwp)

    http://pastebin.com/jhwFFCDu

    Here you go Sir.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this: http://pastebin.com/gbsiAAwx

    Thread Starter prempwp

    (@prempwp)

    keesiemeijer, You sir, are a legend!!! Thank you so very much, it works like a charm now.

    I would like to know what the problem was, theoretically, for example, was there an issue in my save post function??

    😀

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Glad you got it resolved.

    In your original code this line checks if the “option” in the database has the correct value.

    <?php checked('1', get_option('pp_is_feat_meta')); ?>

    While it is saved in the database as a “custom field”:

    update_post_meta($post_id, 'pp_is_feat_meta', $_POST['pp_is_feat_meta']);

    And for both input form fields the name was “name=”pp_is_feat_meta”. For some type of input fields this will only put the last name value in the $_POST array. Thats why I made it an associative array: name="pp_is_feat_meta[featured]"

    I hope this all makes sense.

    Thread Starter prempwp

    (@prempwp)

    well , makes a bit of sense only as of now, but since i am a beginner, can’t expect much from myself… Thanks again.

    Moderator keesiemeijer

    (@keesiemeijer)

    We were all beginners once. keep at it!

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Checkbox issue’ is closed to new replies.