Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
 }
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

$postypes = array('type1', 'type2', 'type3');
foreach ( $postypes as $postype) {

    add_meta_box(
        'video_information',
        'Video Information',
        'video_information',
        $postype
    );
}

You can read more in codex: http://codex.wordpress.org/Function_Reference/add_meta_box

share|improve this answer
    
Thank you @kalymaStudio I added the code you suggested and changed the type1, type2 items to be my custom post types and it works! –  Neelam Khan Oct 4 '13 at 12:43
    
Can you also add a description to your answer, to explain why/how this code works? –  Chip Bennett Oct 4 '13 at 12:51
    
$posttypes is taking the arguments that are in the array. I added my custom post types where it says 'type1' 'type2' and then as far as I know its looping through each custom post type and displaying my meta box in each post type so that users can add the video ID for the video post type as well as the music post type. Which is then displayed on the front end. –  Neelam Khan Oct 4 '13 at 12:58
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

share|improve this answer
    
Can you please add an explanation for why this code works? –  Chip Bennett Oct 4 '13 at 12:16
    
check source just added it –  Mohit Bumb Oct 4 '13 at 12:18
    
Please add a description, and not just code/link. Explain what's different about this code, and how/why it works to answer the OP's question. –  Chip Bennett Oct 4 '13 at 12:19
    
ok I'll give me 10 mins. –  Mohit Bumb Oct 4 '13 at 12:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.