我目前将视频设置为自定义帖子类型,并为其创建了一个自定义元框,允许用户输入youtube/vimeo视频的ID,然后视频将显示在前端。
我想将此元框重新用于另一个自定义帖子类型。我该怎么做?
当前元框的功能是:
// 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
}