Watch youtube video

We are going to define one youtube video per every post. Thus we create  metabox for youtube  video ID.

        function watch_video_id_add_meta_box() {
            $screen = get_current_screen();
            add_meta_box('watch_video_id', 'Youtube Video ID', 'watch_video_id_callback', $screen, 'side', 'default');
        }

        function watch_video_id_callback($post) {
            _F9J_nonce_field('watch_video_save_id_data', 'watch_video_meta_box_nonce');
            $value = get_post_meta($post->ID, 'video_id', true);
            echo '<label for="video_id" >ID </lable>';
            echo '<input type="text" id="video_id" name="video_id" value="' . esc_attr($value) . '" size="20" placeholder = "XXXXXXXXXXX" />';
        }

        add_action('add_meta_boxes', 'watch_video_id_add_meta_box');

Save video ID

function watch_video_save_id_data($post_id) {

            if (!isset($_POST['watch_video_meta_box_nonce'])) {
                return;
            }
            if (!_F9J_verify_nonce($_POST['watch_video_meta_box_nonce'], 'watch_video_save_id_data')) {
                return;
            }
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return;
            }
            if (!current_user_can('edit_post', $post_id)) {
                return;
            }
            if (!isset($_POST['video_id'])) {
                return;
            }

            $my_data = sanitize_text_field($_POST['video_id']);

            update_post_meta($post_id, 'video_id', $my_data);
        }

        add_action('save_post', 'watch_video_save_id_data');

Now we define columns in the dashboard posts list

 function watch_video_set_post_columns() {
            $newColumns               = array();
            $newColumns['title']      = 'Title';
            $newColumns['video_id']   = 'Video_id';
            $newColumns['author']     = 'Author';
            $newColumns['categories'] = 'Categories';
            $newColumns['tags']       = 'Tags';
            $newColumns['date']       = 'Date';
            return $newColumns;
        }

        add_filter('manage_posts_columns', 'watch_video_set_post_columns');

Brows defined columns

function watch_video_posts_custom_column($column, $post_id) {
    switch ($column) {
        case 'title':
            the_title();
            break;
        case 'video_id':
            $video_id = get_post_meta($post_id, 'video_id', true);
            $content  = file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id);
            parse_str($content, $ytarr);
            if (isset($ytarr['title'])) {
               echo '<a href="https://www.youtube.com/watch?v='.$video_id.'" target="_blank">'.$ytarr['title'].'</a>';
            }
            else {
                echo '<a href="https://www.youtube.com/watch?v='.$video_id.'" target="_blank">'.$video_id.'</a>';
            }
            break;
        case 'author':
            the_author();
            break;
        case 'category':
            the_category();
            break;
        case 'tags':
            the_tags();
            break;
    }
}

add_action('manage_posts_custom_column', 'watch_video_posts_custom_column', 10, 2);

Youtube player in single post template (front)

<?php $video_id = get_post_meta($post->ID, 'video_id', true);
            if ( $video_id != "" && isset( $video_id ) ) { ?>
                <iframe id="player" type="text/html" width="320" height="185"
                        src="http://www.youtube.com/embed/<?php echo $video_id; ?>"
                        frameborder="0"></iframe>
           <?php } ?>

 

1 thought on “Watch youtube video”

Leave a Reply

Your email address will not be published. Required fields are marked *