Jump to content

Need some help with customizing this PHP code


jacquesdegatineau

Recommended Posts

Hello.

I want to display one View Count of multiple YouTube videos on my website.

I'm not very tech-savvy, but here's a PHP code that I found on this website. With this code implemented in my website's theme file, and a Shortcode, I was able to display the View Count of one video.

How does this code need to be changed, so it displays the View Counts of multiple videos of my choosing?

Shortcode function:

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');

Shortcode:

[[youtube_view_count id="UKuYgIBnqEA"]]

I would really appreciate any suggestions.

Thank you!

Link to comment
Share on other sites

 @requinix It's 32 videos. That's right, using WordPress I need to use a Shortcode to display the number on the page.

@maxxd Because like I said I want to show the sum of multiple View Counts combined as one number, that's why I was wondering if the function could be re-written.

Edited by jacquesdegatineau
Link to comment
Share on other sites

First thing you have to consider is that you don't want to hit the YouTube API 25 times every time you want these numbers. Wouldn't it be nice if you could get all of them at once?
And you can: take that URL you have and add all the video IDs separated by commas. And you don't need a "id" parameter if you know exactly which videos you want.

Then, since there will be multiple videos in the results, you'll need to total up all the views across all of them. That means a loop.

$views = 0;
foreach ($jsonData->items as $item) {
	$views += $item->statistics->viewCount;
}
Link to comment
Share on other sites

If you look at the actual code, it's pretty simple.   This is the api call from google:

$json = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $videoID . "&key=xxxxxxxxxxxxxxxxxxxxxxxx");

Apparently, if the id = parameter includes a comma delimited list of videos, this api will still work (up to 50 videos).  

 

[[youtube_view_count id="UKuYgIBnqEA,xxxxxxxxx,xxxxxxxx"]]

 

There is an entirely different analytics api you could use, which allows for statistics by "dimension" where your dimensions can be a channel, playlist, video or analytics "group".

You maintain the groups through the youtube studio, and you can have up to 500 videos in a group.  That would be easier to use and maintain long term.   See https://support.google.com/youtube/answer/3529123?hl=en for more details on groups.

Obviously you could get the statistics for the whole channel or add the videos to one or more playlists.

This page lists some sample queries that could be used to get stats for a channel or a group:  https://developers.google.com/youtube/analytics/sample-requests

It looks to me like that is the way you want to go, but I don't have the time to look into it much further other than to suggest a look at:  https://developers.google.com/youtube/analytics

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.