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: 341,970

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: 2,947,558

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!

author bio
Brian Jackson

I craft actionable content and develop performance-driven WordPress plugins. Connect on X, subscribe to my newsletter (once a month), or buy me coffee.

8 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

    Reply
  2. Hi Brian!

    I know you are busy with all things Kinsta, but in case you might see these quick comments still: “Do you happen to know how to get this function/shortcode to work with WordPress 5.3?”

    I just confirmed that it is working on 5.2.4 but does not work on 5.3 (tested on multiple sites in default themes/plugins). It’s over my head, slightly, what would have changed in 5.3 (or perhaps be a bug that may be fixed in 5.3.1) but I wanted to reach out in case you might be able to see test here on woorkup.com or another site you already have on 5.3 to see if you can reproduce the error, and then see if you might be able to figure out what it is that might need a quick change. Or perhaps your brother knows and could figure it out in 5min, ha. :-)

    Thanks so much for replying/considering a quick test whenever you might be able (if ever able), and I hope things are going well as you maintain so many of the cool things you’ve got spinning!

    Thanks, again! :-)

    Reply
    • Hey Caleb! I have more time now, so catching up on comments. :)

      The above still works. The shortcodes are actually live data in the post above, so you can see they are pulling the view counts directly from the videos. I haven’t changed anything since 2016 in regards to the content. Did you ever give this another try?

      Reply

Leave a Comment

1