Use YouTube video ID for thumbnails and embed in WordPress

In this tutorial, I show how to use any YouTube video URL to pull the YouTube thumbnail and also how to embed the video in your post.

If you post a lot of YouTube videos and create featured images for each one, this tutorial may save you some time. Instead of having to created featured images for your posts, I show you have you can use the YouTube video ID to pull the thumbnail that YouTube provides.

Get the video ID from YouTube URL

Below is the code that I put into my extras.php file that pulls the video ID from a YouTube URL:

To use this code, simply pass a URL into function like the example below.

Display the YouTube video thumbnail or the Featured Image

Below is the code that checks to see if there is a URL in the meta box. If there is, it displays the YouTube video thumbnail. If not, it displays the uploaded featured image from the post.

Embed the YouTube video on the single post layout

Now that we have the YouTube thumbnail added to our theme, we now want to use the code below to embed the video on our single post layout.

Mentions in the Video Tutorial


I have mentioned it in other videos, but it is always a good idea to prefix your custom functions with something unique. In my case, I use helpwp_. This is to make sure when you are adding plugins and other functionality to your theme that there isn’t another function that is named the same. Notice in the video when I added the custom function to pull the YouTube video ID that I didn’t prefix the custom function. I have updated the code example above that includes my theme prefix to fix this issue.

I didn’t mention this in the video tutorial, but it is always a good idea, SEO wise, to have alt tags on your images. You will notice in the video when I am adding the YouTube thumbnail that I didn’t have an alt tag on the image. I have updated the example code above to include an alt tag that uses the title of the post ( the_title(); ).