How to get Youtube Views count

Published Jul 29, 2017

Paste the below function in your wordpress functions.php or in any php page and replae the key value with the google api key for youtube data API and the video id with the youtube video id and Voila you get the total number of views for the youtube video 😃 😃

function youtube_view_count_shortcode($params)
 $videoID = $params['id']; // view id here 
 $json = file_get_contents("" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");
 $jsonData = json_decode($json);
 $views = $jsonData->items[0]->statistics->viewCount;
 return number_format($views);

if WordPress, add this below line as well

add_shortcode('youtube_view_count', 'youtube_view_count_shortcode');
Discover and read more posts from RajhaRajesuwari S
get started