How do I display Youtube/Vimeo video ID on custom post type when user enters it in custom meta box?

Do you have custom post type videos? if not change videos to post

add_meta_box( 'my-meta-box-id', 'Enter Video ID', 'cd_meta_box_cb', 'post', 'normal', 'high' );

Your code is fine but you are not saving the video type select box you just updated the id input field.

Place this code in add_action('save_post', 'cd_meta_box_save');

if( isset( $_POST['my_meta_box_text'] ) && isset( $_POST['my_video_type'] ) ) {
        update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
        update_post_meta( $post_id, 'my_video_type', strip_tags($_POST['my_video_type']) );
    }

It will also save your selected video type and after try echo with the following code.

EDITED:

global $post;
if (get_post_meta($post->ID, 'my_video_type', true) == "youtube") {
    echo do_shortcode('[youtube id="'.get_post_meta($post->ID, 'my_meta_box_text', true).'"]');
}
if (get_post_meta($post->ID, 'my_video_type', true) == "vimeo") {
    echo do_shortcode('[vimeo id="'.get_post_meta($post->ID, 'my_meta_box_text', true).'"]');
}

Youtube and Vimeo Shortcode:

function wp_youtube_video($atts) {
    extract(shortcode_atts(array('id' => ''), $atts));
    return '<iframe title="YouTube video player" class="youtube-player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/'.$id.'" frameborder="0" allowFullScreen></iframe>';
}

add_shortcode('youtube', 'wp_youtube_video');

    function wp_vimeo_video($atts) {
        extract(shortcode_atts(array('id' => ''), $atts));
        return '<iframe src="http://player.vimeo.com/video/'.$id.'" width="WIDTH" height="HEIGHT" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
    }
    add_shortcode('vimeo', 'wp_vimeo_video');

Leave a Comment