Add Meta Boxes not saving or retrieving
-
Hi,
I found a website that provides sample code to add_meta_box functions. After copying and pasting and making some changes so I can get it to work with the “Post” type, instead of a Custom Post Type, it worked like a charm. Started to add some more input fields to save additional data, and now it doesn’t save any data. For that reason I commented out any textboxes that I created, it still won’t save or retrieve the data.
Here’s the functions.php. Please let me know if you find something wrong.
/******************************************************************************/
add_action( ‘add_meta_boxes’, ‘ahng_add_meta_box’ );
add_action( ‘save_post’, ‘ahng_save_postdata’ );function ahng_add_meta_box() {
add_meta_box(
‘ahng_thumb_meta_box’,
‘Video Options’,
‘ahng_video_options_meta_box_func’,
‘post’
);
}function ahng_video_options_meta_box_func( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), ‘ahng_noncename’ );$mydata = get_post_meta($post->ID, ‘post’, TRUE);
// The actual fields for data entry
echo ‘<label for=”ahng_url_thumbnail”>’;
echo ‘URL to thumbnail:’;
echo ‘</label>’;
echo ‘<input type=”text” id=”ahng_url_thumbnail” name=”ahng_url_thumbnail” value=”‘.$mydata[‘ahng_url_thumbnail’].'” size=”50″ />’;
echo ‘<label for=”ahng_duration_time”>’;
echo ‘Duration Time: Ex,(1:22)(11:34)(0:25)’;
echo ‘</label>’;
echo ‘<input type=”text” id=”ahng_duration_time” name=”ahng_duration_time” value=”‘.$mydata[‘ahng_duration_time’].'” size=”15″ />’;
//echo ‘‘;
//echo ‘<label for=”ahng_itunes_aff”>’;
//echo ‘Itunes Affiliate Link (URL)’;
//echo ‘</label>’;
//echo ‘<input type=”text” id=”ahng_itunes_aff” name=”ahng_itunes_aff” value=”‘.$mydata[‘ahng_itunes_aff’].'” size =”75″ />’;
//echo ‘‘;
//echo ‘<label for=”ahng_amazon_aff”>’;
//echo ‘Amazon Affiliate Link (URL)’;
//echo ‘</label>’;
//echo ‘<input type=”text” id=”ahng_amazon_aff” name=”ahng_amazon_aff” value=”‘.$mydata[‘ahng_amazon_aff’].'” size =”73″ />’;//Add more fields as you need them…
}function ahng_save_postdata( $post_id ) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
echo ‘doing autosave’;
return;if ( !wp_verify_nonce( $_POST[‘ahng_noncename’], plugin_basename( __FILE__ ) ) )
echo ‘can not verify nonce’;
return;if ( !current_user_can( ‘edit_post’, $post_id ) )
echo ‘user can not make changes’;
return;$mydata = array();
foreach($_POST as $key => $data) {
if($key == ‘ahng_noncename’)
continue;
if(preg_match(‘/^nivo/i’, $key)) {
$mydata[$key] = $data;
}
}
update_post_meta($post_id, ‘post’, $mydata);
return $mydata;
}?>
- The topic ‘Add Meta Boxes not saving or retrieving’ is closed to new replies.