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.