I currently have videos set up as a custom post type and I have also created a custom meta box for it that allows users to enter the ID of either a youtube/vimeo video – the video is then displayed on the front end.
I want to re-use this meta box for another custom post type. How would I do this?
The function for the current meta box is:
// Create the Video Information Meta Box by hooking into the admin menu for a post
add_action('admin_menu', 'video_add_box');
function video_add_box(){
add_meta_box('video_information', 'Video Information', 'video_information', 'videos', 'normal', 'high');
}
//function to populate the meta box added above
function video_information(){
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="video_noncename" id="video_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
//adds the custom field _youtubeID plus some other stuff
$youtubeID = get_post_meta($post->ID, '_youtubeID', true);
if ( empty($youtubeID) ) {
$youtubeID = '';
}
//adds the custom field _vimeoID
$vimeoID = get_post_meta($post->ID, '_vimeoID', true);
if ( empty($vimeoID) ) {
$vimeoID = '';
}
//add the box
echo '<br />';
echo '<strong>Youtube ID:</strong> <input type="text" name="_youtubeID" value="' . $youtubeID . '" size="20" maxlength="30" />';
echo '<br />';
echo '<strong>Vimeo ID:</strong> <input type="text" name="_vimeoID" value="' . $vimeoID . '" size="20" maxlength="30" />';
echo '<br />';
} //end video_information function
//save_video_meta is called below with the action "save_post" and saves your IDs to the post
function save_video_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['video_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID )){
return $post->ID;
}
$video_meta['_youtubeID'] = $_POST['_youtubeID'];
$video_meta['_vimeoID'] = $_POST['_vimeoID'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV
if($value) {
update_post_meta($post_id, $key, $value);
} else
delete_post_meta($post_id, $key); // Delete if blank
}
} //end save_video_meta
//save the video custom fields
add_action( 'save_post', 'my_save_postdata' );
function my_save_postdata($post_id){
$video_meta['_youtubeID'] = $_POST['_youtubeID'];
$video_meta['_vimeoID'] = $_POST['_vimeoID'];
foreach ($video_meta as $key => $value) { // Cycle through the $video_meta array
if( $_POST['post_type'] == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV
if(get_post_meta($post_id, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post_id, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post_id, $key, $value);
}
if(!$value) delete_post_meta($post_id, $key); // Delete if blank
}//endforeach video meta
}
Yes, you need to edit your video_add_box() function in order to make an array with the post type you need to show that box.
You can read more in codex: http://codex.wordpress.org/Function_Reference/add_meta_box
Source : http://wordpress.org/ideas/topic/add-meta-box-to-multiple-post-types