Get YouTube Views with PHP or WordPress Shortcode

Like everything Google, there’s very little that the YouTube API doesn’t offer. However, the following PHP function (and WordPress shortcode) will do nothing other than retrieve the view count given a video ID. Having just built a cool YouTube application I’ll be sure to post some code sometime in the future.

YouTube Video Count Shortcode

As a simple example, and only if used in WordPress, the shortcode of [ytviews video="B1a6VddQgxA"] will simply render the text of . The YouTube video ID as used in the shortcode can be found in the URL address bar of your browser (http://www.youtube.com/watch?v=B1a6VddQgxA).

Video ID

Video ID sourced from YouTube URL

Available Shortcode Attributes

video – This is the ID of the video you want to retrieve counts for
cache – How long you want to cache the results locally (on your end). Defaults to 12 hours. The API lags a little behind actual view.

You should copy the following code into your theme’s functions.php file or your custom shortcode plugin.

function getYoutubeViews($atts) {
    extract(shortcode_atts(array(
    'video' => '',
    'cache' => '43200' // 12 hours (60*60*12)
  ), $atts));
	$myYTviews = 'YTV_' . $video;
	$cachedposts = get_transient($myYTviews);
	if ($cachedposts !== false) {
	return $cachedposts;
	} else {
	$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos?q=$video&alt=json");
	$JSON_Data = json_decode($JSON);
	$views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
	$return = $views;
    set_transient($myYTviews, $return, $cache);
   return $return;
 }
}
add_shortcode('ytviews','getYoutubeViews');

PHP Function

Used outside of WordPress, the following PHP function applies.

<?php
function get_youtube_views($video_ID) {
	$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos?q={$video_ID}&alt=json");
	$JSON_Data = json_decode($JSON);
	$views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
	echo $views;
	}
get_youtube_views('B1a6VddQgxA');
?>

If you have any problems, please let me know.

First Name:
Your Email Address:
 




Download: Get YouTube Video Views
Description: Count YouTube Views with PHP and WordPress shortcode
Author:Marty
Category: PHP code
Date: May 19, 2012



If you liked this article, you may also like:

  1. Insert YouTube Subscriber Count or Video Views in WordPress with Shortcode
  2. Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode
  3. Replace YouTube Links in Text with Embed Code (and Retrieve Video Details via the JSON API)
  4. YouTube Thumbnails in WordPress Posts with Shortcode
  5. [Shortcode] Insert Youtube videos into your blog with shortcode
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Comments

  1. Sarquiz says:

    Hi Marty, I can’t get the php function to work :( I applied it just like it’s in your file.

    • Marty says:

      If you send a copy of your functions.php file I’ll see if there are errors. Silly point… but make sure you’ve entered the shortcode in the HTML view of your post editor (not in the rich editor).

  2. Lalit says:

    Code given by you is working great. Code given to use in PHP is showing views data before 12 hours. Our requirement is like we want to show current Youtube views count.
    Thank in advance.

  3. jim says:

    Marty, still working on getting a script to work as i want. I’ve noticed that if video urls have a – in it, like this one “s82-FyiPnE8″ it won’t display properly.

    I’ve put my code up here, and i get various inconsistencies: http://pastebin.com/gb91WT4G

    if you use this with the following shorcodes in a post, you should see how some don’t work properly because of the “-” in the url:

    [ytviews video="s82-FyiPnE8"]
    [ytviews video="ARceRM8xHtc"]
    [ytviews video="-pqrSdbQww8"]
    [ytviews video="DxLZ1jCw4O4"]

    Any ideas as to what I’m doing wrong?

Trackbacks

  1. [...] allow you to do. All we want to do in this case, however, is obtain the video thumbnail, title, views, description, author and [...]

  2. [...] We’ve previously looked at how to include YouTube video views into your WordPress post or page with shortcode; this post will show you how to include your [...]

Speak Your Mind

*