0

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.

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, '/>';
  • 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 ) – Adam 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? – Adam Oct 18 '12 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.