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 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() {
    add_meta_box(
        'movie_meta_box',
        'Movie Meta Options',
        'movie_options_meta_box_func',
        'movie'
    );
}
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 )
      return;
  if ( !wp_verify_nonce( $_POST['movie_noncename'], plugin_basename( __FILE__ ) ) )
      return;
  if ( !current_user_can( 'edit_post', $post_id ) )
      return;
  $mydata = array();
  foreach($_POST as $key => $data) {
    if($key == 'movie_noncename')
      continue;
    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

1 Answer 1

Change,

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

to...

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

...and let us know how you go.

Update

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

 
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.