Title: Checkbox issue
Last modified: August 20, 2016

---

# Checkbox issue

 *  [prempwp](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775212)
 * 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](http://codex.wordpress.org/Function_Reference/checked)
 *  Thread Starter [prempwp](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775253)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775254)
 * 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](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775260)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775342)
 * 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](http://pastebin.com/)
   and post the link to it here? see the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code)
   for posting code and using the pastebin.
 *  Thread Starter [prempwp](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775349)
 * `http://pastebin.com/jhwFFCDu`
 * Here you go Sir.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775354)
 * Try it with this: [http://pastebin.com/gbsiAAwx](http://pastebin.com/gbsiAAwx)
 *  Thread Starter [prempwp](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775357)
 * **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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775359)
 * 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](https://wordpress.org/support/users/prempwp/)
 * (@prempwp)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775360)
 * 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](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775361)
 * 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.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * Last activity: [13 years, 12 months ago](https://wordpress.org/support/topic/checkbox-issue/#post-2775361)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
