J'ai actuellement des vidéos configurées en tant que type de publication personnalisée et j'ai également créé une méta-boîte personnalisée qui permet aux utilisateurs de saisir l'ID d'une vidéo youtube/vimeo - la vidéo est ensuite affichée au début.
Je souhaite réutiliser cette boîte méta pour un autre type de message personnalisé. Comment je ferais ça?
La fonction de la meta box actuelle est:
// 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
}
Oui, vous devez éditer votre fonction video_add_box () afin de créer un tableau avec le type de post dont vous avez besoin pour afficher cette boîte.
$postypes = array('type1', 'type2', 'type3');
foreach ( $postypes as $postype) {
add_meta_box(
'video_information',
'Video Information',
'video_information',
$postype
);
}
Vous pouvez en lire plus dans le codex: http://codex.wordpress.org/Function_Reference/add_meta_box
add_action( 'add_meta_boxes', 'my_add_custom_box' );
function my_add_custom_box($postType) {
$types = array('type1', 'type2', 'type3');
if(in_array($postType, $types)){
add_meta_box(
'myid',
__( 'Title', 'myplugin_textdomain' ),
'callback',
$postType
);
}
}
Source: http://wordpress.org/ideas/topic/add-meta-box-to-multiple-post-types