Dariusz
Dariusz

Reputation: 303

Wordpress meta_box checkbox update is not updating

I have a problem with updating my custom meta box in wordpress for a custom post news. I have made a meta box checkbox so I could decide if the post is featured or not. If the checkbox is checked then isFeatured = 1. Until this part it works, the problem is when after sometime I wan't to edit the post and deselect the checkbox. After updating the post the checkbox is checked all the time.

The database: meta_key | meta_value ---- > news_details | a:1:{s:10:"isFeatured";s:1:"1";}

This is how my code looks:

function add_news_details_meta_box() {
    add_meta_box(
        'news_details_meta_box', // $id
        'Checkbox', // $title
        'show_news_details_meta_box', // $callback
        'news', // $screen
        'normal', // $context
        'high' // $priority
    );
}
add_action( 'add_meta_boxes', 'add_news_details_meta_box' );

function show_news_details_meta_box() {
    global $post;
        $meta = get_post_meta( $post->ID, 'news_details', true ); ?>

  <input type="hidden" name="news_details_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <p>
    <label for="news_details[isFeatured]">Is this post featured?
      <input type="checkbox" name="news_details[isFeatured]" value="1" <?php if (is_array($meta) && $meta['isFeatured'] === '1' ) { echo 'checked';} else { echo 'Unchecked';}?>>
    </label>
  </p>

  <?php
}
function save_news_details_meta( $post_id ) {
    // verify nonce
    if ( !wp_verify_nonce( $_POST['news_details_meta_box_nonce'], basename(__FILE__) ) ) {
        return $post_id;
    }
    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // check permissions
    if ( 'page' === $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post_id ) ) {
            return $post_id;
        } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    }

    $old = get_post_meta( $post_id, 'news_details', true );
    $new = $_POST['news_details'];
    if ( $new && $new !== $old ) {
        update_post_meta( $post_id, 'news_details', $new );
    } elseif ( '' === $new && $old ) {
        delete_post_meta( $post_id, 'news_details', $old );
    }
}
add_action( 'save_post', 'save_news_details_meta' ); 

Could you explain to me what's wrong with updating the checkbox after sometime?

Upvotes: 0

Views: 331

Answers (2)

Dariusz
Dariusz

Reputation: 303

What worked for me in this case was changing } elseif ( '' === $new && $old ) { to } else ( '' === $new && $old ) {

Upvotes: 0

rank
rank

Reputation: 2534

I think you should check the if condition. The way I got your description, the $meta['isFeatured'] === '1' always becomes true. You set the value to 1 in the input tag. So on save the value of 1 gets saved to the field I guess. On next load you are checking with an if, if the $meta['isFeatured'] is 1, so this is always true.

You could check if the checkbox in the form is checked with:

isset($_POST['name_of_your_checkbox_input'])

If it is set, you save the value of 1. If not, you stay at the pre defined value of 0.

Upvotes: 1

Related Questions