How to Show YouTube Video View Counts in WordPress

Follow the tutorial below on how to display YouTube video view counts in WordPress on any page, post, or custom post type. You will end up with a shortcode that you can easily use wherever you want. This tutorial uses the latest YouTube Data API (v3) as v2 is now deprecated.

Show YouTube View Counts in WordPress

I have now updated this post to include two different options. You can now create a shortcode to show individual video view counts as well as a total count of a YouTube channel. This can be cool to show on a homepage or widget.

Option 1 – Show YouTube Individual Video View Counts

Step 1

First you are going to need an API key from Google. So go to Google Developers Console and login with your Google account.

Step 2

Create the new project (name does not matter).

google developer console create project

Step 3

Click into your new project from the dropdown. In the sidebar on the left, expand APIs & auth. Next, click APIs. In the list of APIs go to YouTube Data API (v3) and make sure that it’s enabled (Enable API).

youtube data api

Step 4

In the same sidebar click Credentials. Then click on “Add credentials” and choose “API key.”

add credentials api key

Step 5

Then click on “Browser Key.”

browser key

Step 6

Under referrers simply leave it blank and click on “Create.”

Step 7

On the next screen you will see a popup with your new API key. Copy this as you will need it later on.

api key youtube

Step 8

Now we need to create your shortcode in WordPress. So click into “Appearance” and then into “Editor.”

wp appearance editor

Step 9

Then click into your functions.php file.

theme functions

Step 10

Now add the code below to the bottom of your functions.php file. Note: Please make a backup first if you aren’t familiar with editing this file. Big kudos to my brother who quickly wrote this up in about 5 minutes. I was needing it for a project I am working on over at 92 Keys.

Replace the xxxxxxxxxxxxxxxxxxxxxxx after $key= with your API key.

function youtube_view_count_shortcode($params)
{
 $videoID = $params['id'];
 $json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");
 $jsonData = json_decode($json);
 $views = $jsonData->items[0]->statistics->viewCount;
 return number_format($views);
}
add_shortcode('youtube_view_count', 'youtube_view_count_shortcode');

You now have a shortcode you can use in WordPress!

Step 11

Browse to the page, post, or custom post type you want to display the YouTube video views counts on and input the following shortcode. Replace the ID with the video ID from YouTube.

[youtube_view_count id="UKuYgIBnqEA"]

You can grab the video’s ID by looking in your address bar.

youtube video id

Example of YouTube Video View Counts

As you can see above I have the YouTube video… feel free to click into it and view the count over at YouTube. Also take a minute and like the video as it is part of a project I am involved in over at 92 Keys. You can compare it to what the shortcode is outputting below. Please note, it might be a couple off as I think it caches a little bit… but should be fairly close.

View Count: 327,871

Option 2 – Show Total YouTube Channel View Counts

Step 1

You will want to repeat the exact same process above up through step 10. You will need to create a new function for a channel shortcode.

Replace the xxxxxxxxxxxxxxxxxxxxxxx after $key= with your API key.

function youtube_view_count_channel_shortcode($params)
{
 $channelID = $params['id'];
 $json = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" . $channelID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");
 $jsonData = json_decode($json);
 $views = $jsonData->items[0]->statistics->viewCount;
 return number_format($views);
}
add_shortcode('youtube_view_count_channel', 'youtube_view_count_channel_shortcode');

Step 2

Browse to the page, post, or custom post type you want to display the YouTube channel view counts on and input the following shortcode. Replace the ID with your channel’s ID from YouTube.

[youtube_view_count_channel id="UCdXJhAstZc8y3G-4uCaa64g"]

Example of YouTube Channel View Counts

Channel Count: 1,761,077

You can then style the shortcode with CSS and make it look like however you want. If this tutorial on how to show YouTube video view counts in WordPress (and channel view counts) was helpful please let me know below in the comments!

Get More Stuff Like This

Marketing, WordPress, Blogging Tips, SEO and Reviews, once a week.

Thank you for subscribing.

Something went wrong.

5 thoughts on “How to Show YouTube Video View Counts in WordPress”

  1. Sorry for the previous mistake.

    I did try to do exactly what written in this page; but, it doesn’t work.
    As you can see in this page http://test.ipase.com/?page_id=7 , the result is always zero.
    In the first line the total number of video view in my You Tube channel (“ipaseteam”), then other about 30 lines, where are written the title of my video, where I would like, for each, display the view count (i have tried only for the first).
    I am using Word Press with Mantra Theme; I have the API key.
    Attached the code added (cut and paste from your) at the end of Theme Functions.

    Any suggestion?

    Thanks in advance,

    Paolo

Leave a Comment