Codementor Events

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("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);
}

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
post commentsBe the first to share your opinion
Angerla Nguyen
5 years ago

Thanks

Ninja Nima
5 years ago

Hi my name is nima can u plz use this method in my channel plzzzzzz https://youtu.be/CfgCr64oUzg

Ninja Nima
5 years ago

I want to see if it will work

RajhaRajesuwari S
5 years ago

Hi this is part of coding. do u have a website to list ur youtube?

Ninja Nima
5 years ago

what website i do now understand

Ninja Nima
5 years ago

can u increase views

Show more replies