WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a checkbox above my metabox in a custom post. When I check it and publish/update the post, it become unchecked again meaning it's not saving it's value. Here is my code for the checkbox:

add_action('add_meta_boxes', 'my_meta_box'); 
add_action('save_post', 'save_postdata');   
function my_meta_box() {
        'Movie Meta Options',
function movie_options_meta_box_func( $post ) {
  wp_nonce_field( plugin_basename( __FILE__ ), 'movie_noncename' );
  $mydata = get_post_meta($post->ID, 'movie', TRUE);
  echo '<input type="checkbox" id="movie_abc" name="movie_abc" value="true" />';
  echo '<input type="text" id="movie_xyz" name="movie_xyz" value="'.$mydata['movie_xyz'].'" size="60" />';
function save_postdata( $post_id ) {
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  if ( !wp_verify_nonce( $_POST['movie_noncename'], plugin_basename( __FILE__ ) ) )
  if ( !current_user_can( 'edit_post', $post_id ) )
  $mydata = array();
  foreach($_POST as $key => $data) {
    if($key == 'movie_noncename')
    if(preg_match('/^/i', $key)) 
      $mydata[$key] = $data;
  update_post_meta($post_id, 'movie', $mydata);
  return $mydata;

Your help will be highly appreciated.

share|improve this question


echo '<input type="checkbox" id="movie_abc" name="movie_abc" value="true" />';


echo '<input type="checkbox" id="movie_abc" name="movie_abc" value="1"' . checked( $mydata , 1 ) . '/>';

...and let us know how you go.


Try the following,

echo '<input type="checkbox" id="movie_abc" name="movie_abc" value="1"', $mydata ? ' checked="checked"' : '', '/>';

Update 2

This is a duplicate question;

I now see in your code that you are not updating the meta_key values for,

movie_abc and movie_xyz

So you need to add,

update_post_meta($post_id, 'movie_abc', $mydata); //current value of this key is "true" only?
update_post_meta($post_id, 'movie_xyz', $mydata);

...to your save_postdata() function.

Update 3

$mydata = get_post_meta($post->ID, 'movie', TRUE);
if($mydata  == true)
$checked  = 'checked="checked"'; 
echo '<input type="checkbox" id="movie_abc" name="movie_abc" value=""', $checked, '/>';
share|improve this answer
thanks for you answer. I have implemented it but it's not working. – user1666698 Oct 17 '12 at 8:37
What happens if you change, checked( $mydata , 1 ) to (checked( $mydata , 1 )) - add extra brackets around side or alternatively checked( $mydata , 1, false ) – userabuser Oct 17 '12 at 8:50
I have tried both with both- that is- with parentheses and with false, but it didn't work. – user1666698 Oct 17 '12 at 9:05
About saving of meta key value: Actually if you see I'm not saving meta key value for text input but I manage to save its value so I think that's not the matter. – user1666698 Oct 18 '12 at 18:22
What is the point of movie_abc - I don't follow your logic, it doesn't appear to be passed into your function, and the point of your regex? – userabuser Oct 18 '12 at 18:36

Your Answer


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.