Featured Image Import YouTube & Vimeo Video Covers Into WordPress Media Library

Import YouTube & Vimeo Video Covers Into WordPress Media Library

Cabe Nolan Author
Cabe Nolan October 29, 2018


This post was originally created on our sister site WP Cover.
View Original Article

We had a client that was re-designing their site and it incorporated a lot of video. The previous design of the site used video embed codes but the new design was set to show video in a lightbox popup. Due to this change in functionality, there had to be a clickable image that pulled up the video lightbox.

Trying to go through thousands of programs within this website and adding a video cover would have taken forever. Therefore, we wrote a plugin that fetched the Vimeo or YouTube video cover image, uploaded it to the WordPress media library, and then set it as a video cover to our defined Advanced Custom Fields field.

We used two different guides that were previously shared on WP Cover as a base for developing this functionality. The first was our ‘Do Random Things‘ plugin which is just a framework for creating plugins quickly. The second was our plugin for Importing Images from Flickr into WordPress.

I’ll explain the code quickly and then provide the full plugin below. Here’s what we’re doing:

  • Grabbing the video link and determining if it is a YouTube or Vimeo video.
  • Based on that information, we’re grabbing the static image file for the video.  The method for doing this is different based on whether the video is from YouTube or Vimeo.  In this case, we’re grabbing the highest resolution image file but there are other options.
  • If we’re successful in grabbing the URL, we’re going to save it to our wp-uploads folder and add it to the media library using wp_insert_attachment.
  • Finally, we’re going to assign it to the necessary Advanced Custom Fields field already setup.

Here is the full source of the plugin.  Enjoy!



Import Video Covers

Click Process button to run through posts and import data.

-1, 'post_type' => 'programs', ); $the_query = new WP_Query( $args ); if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post(); $parent_post_id = get_the_ID(); if(get_field('video_embed') && !get_field('video_cover')) { $i++; $vidembed = get_field('video_embed'); $coversrc= ''; if (strpos($vidembed, 'youtube') > 0) { $type = 'youtube'; $explode = explode('v=', $vidembed); $youtubekey = $explode[1]; $coversrc = 'https://img.youtube.com/vi/' . $youtubekey . '/maxresdefault.jpg'; } else { $type = 'vimeo'; $explode = explode('vimeo.com/', $vidembed); $imgid = $explode[1]; $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php")); $coversrc = $hash[0]['thumbnail_large']; } if(!empty($coversrc)) { $filename = rand(10000, 999999999) . '.jpg'; $uploaddir = wp_upload_dir(); $uploadfile = $uploaddir['path'] . '/' . $filename; $contents= file_get_contents($coversrc); if(empty($contents)) { $failedgal[] = $parent_post_id; } $savefile = fopen($uploadfile, 'w'); fwrite($savefile, $contents); fclose($savefile); $wp_filetype = wp_check_filetype(basename($filename), null ); $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . $filename, 'post_mime_type' => $wp_filetype['type'], 'post_title' => $filename, 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $uploadfile, $parent_post_id ); // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile ); wp_update_attachment_metadata( $attach_id, $attach_data ); update_field('video_cover', $attach_id); } } endwhile; endif; wp_reset_query(); } ?>

The post Import YouTube & Vimeo Video Covers Into WordPress Media Library appeared first on WP Cover.


Cabe Nolan Author Image
Written by

Cabe Nolan