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 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.
|
|
Download: Get YouTube Video Views |
If you liked this article, you may also like:
- Insert YouTube Subscriber Count or Video Views in WordPress with Shortcode
- Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode
- Replace YouTube Links in Text with Embed Code (and Retrieve Video Details via the JSON API)
- YouTube Thumbnails in WordPress Posts with Shortcode
- [Shortcode] Insert Youtube videos into your blog with shortcode


Hi Marty, I can’t get the php function to work :( I applied it just like it’s in your file.
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).
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.
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?