YouTube thumbnail from video url

/**
 * Get YouTube thumbnail from video url 
 *
 */
function be_youtube_thumbnail( $video_url ) {
  $y_id = preg_replace( '~(?:http|https|)(?::\/\/|)(?:www.|)(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[a-z0-9;:@#?&%=+\/\$_.-]*~i', '$1', $video_url );
  $image = $y_id ? '<img src="https://img.youtube.com/vi/' . $y_id . '/0.jpg" />' : false;
  return $image;
}

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk