WordPress custom field checkbox always remains checked

This is really frustrating, I am trying to setup a custom field with a checkbox and no matter what I do the checkbox always remains checked even after unchecking it and updating the post…here is my code:

add_action('add_meta_boxes', 'jpg_affiliated');
function jpg_affiliated()
{
	add_meta_box('jpg_affiliated_staff', 'Who Was Involved?', 'jpg_affiliated_staff_callback', 'post', 'normal', 'default');	
}
function jpg_affiliated_staff_callback($post)
{
	global $post;
    $custom = get_post_custom($post->ID);
	wp_nonce_field('jpg_affiliated_nonce', 'jpg_affiliated_nonce_field');
?>
	

Select any staff that were involved in this event and what their role was.

ID,'jpg_team_member', true ) == "Test" ){echo ' checked="checked"';} ?>> Test Select role... Primary Shooter Second Shooter Third Shooter Lighting Assistant Photo Booth Coordinator Marketing Coordinator Studio Manager JPG Team Member Photographer
<?php } add_action('save_post', 'jpg_save_staff'); function jpg_save_staff($post_id) { // Bail if we're doing an auto save if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; // if our nonce isn't there, or we can't verify it, bail if(!isset($_POST['jpg_affiliated_nonce_field']) || !wp_verify_nonce($_POST['jpg_affiliated_nonce_field'], 'jpg_affiliated_nonce' )) return; // if our current user can't edit this post, bail if(!current_user_can('edit_post')) return; // Probably a good idea to make sure your data is set if(isset($_POST['jpg_team_member'])){update_post_meta($post_ID, 'jpg_team_member', esc_attr($_POST['jpg_team_member']));} }

On the first look, seems like you forgot to add name attribte to select field. Jsut add ‘jpg_team_member’ to the name of the

It may be from this:

value="Test"<?php if(get_post_meta($post->ID,'jpg_team_member', true ) == "Test" ){echo ' checked="checked"';} ?>

You setup the value as “Test” then check if that is equal to “Test”, of course that is true and you have then checked=“checked”.

This is what i see:
a = 5;
if (a == 5 ) echo “checked” :slight_smile:

Just don’t setup that a as 5… in your case, as Test

It’s because you only save his state only if you get true from this thing :

isset($_POST['jpg_team_member'])

You also change his value if that thing is false. So… add there an else statement that changes it to false.

But don’t we have to check for that value to apply the “checked”? even without it, it’s still remaining checked:

ID,'jpg_team_member', true )){echo ' checked="checked"';}  ?>>
wickedpixel said

It’s because you only save his state only if you get true from this thing :

isset($_POST['jpg_team_member'])

You also change his value if that thing is false. So… add there an else statement that changes it to false.

I don’t understand what you mean, can you please provide an example that would be helpful to myself as well as others reading this, please.

Ok here you go: http://pastebin.com/TgYDzxQF

What happened:

You currently have value of that field Test in your database. And in your saving syntax, you have function that updates that value only if the checkbox is checked. That means, if you uncheck checkbox and save, value will not update, and if you leave it checked, it will just save Test again.

So I have included else statement that leaves that field empty if checkbox is not set.

Thank you for that, unfortunately it still isn’t working, now it doesn’t save the checkbox at all, it just shows unchecked at all times.

Sorry but I don’t see the problem now, maybe someone else can help, I lost my eyes searching for mistake :stuck_out_tongue:


needs to be


?

wickedpixel said

needs to be


?

I am working on the checkbox at the moment, not the select menu.

wickedpixel said

needs to be


?

Nope, that’s not important now, that name is of checkbox above that select. It’s not saving properly.