Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I trying to set a meta_box with a single checkbox, everything goes fine, however if I uncheck it and save the post, it marks again as checked, I've been taking a look but I cannot find my mistake.

Take a look a my code.

 function am_checkbox_option() {
        global $post;
        $custom = get_post_custom($post->ID);
        $front_event = $custom["front_event"][0];
        wp_nonce_field(__FILE__, 'am_front_event');
          if( $front_event ){ $checked = "checked=\"checked\""; }else{ $checked = "";}
          ?>
          <label>Display Content? (type yes):</label>
          <input type="checkbox" name="front_event" value="true" <?php echo $checked; ?> />
          <?php
        }
});

 add_action('save_post', function() {
if ( defined( 'DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;

global $post;

 if ( $_POST && !wp_verify_nonce($_POST['am_front_event'], __FILE__) ) {
    return;
}

 if ( isset($_POST['front_event']) ) {
    update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}

 });

Thanks in advance

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Here is code I have used before - the main difference looks to me that you are checking if the meta exists rather than what it's value is to determine if it should be checked.

// Checkbox Meta
add_action("admin_init", "checkbox_init");

function checkbox_init(){
  add_meta_box("checkbox", "Checkbox", "checkbox", "post", "normal", "high");
}

function checkbox(){
  global $post;
  $custom = get_post_custom($post->ID);
  $field_id = $custom["field_id"][0];
 ?>

  <label>Check for yes</label>
  <?php $field_id_value = get_post_meta($post->ID, 'field_id', true);
  if($field_id_value == "yes") $field_id_checked = 'checked="checked"'; ?>
    <input type="checkbox" name="field_id" value="yes" <?php echo $field_id_checked; ?> />
  <?php

}

// Save Meta Details
add_action('save_post', 'save_details');

function save_details(){
  global $post;

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post->ID;
}

  update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}
share|improve this answer
    
Also this solution works fine! Thanks Steph –  andresmijares25 Nov 16 '11 at 2:45
    
Use the add_meta_boxes action for adding metaboxes(it's there specifically for that), as per the example on the add_metabox codex page. You'll also benefit in getting the post type and post object passed to the callback. –  t31os Nov 16 '11 at 12:37

simple add an else clause to delete the post meta if not checked and your code will do just fine, so change :

if ( isset($_POST['front_event']) ) {
    update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}

to

if ( isset($_POST['front_event']) ) {
    update_post_meta($post->ID, 'front_event', $_POST['front_event']);
}else{
    delete_post_meta($post->ID, 'front_event');
}
share|improve this answer
2  
if the checkbox is unchecked, it will not be in the $_POST array, it's only sent when checked, hence why the else statement works. –  Tom J Nowell Nov 15 '11 at 23:21
    
worked perfect man, Thanks a ton! –  andresmijares25 Nov 16 '11 at 2:44

Your Answer

 
discard

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.