Created
October 27, 2017 13:57
-
-
Save celticwebdesign/86cd512dc8865eaaad34c45a413e5da0 to your computer and use it in GitHub Desktop.
A WordPress plugin to get all videos from a YouTube Channel, adds up all video view counts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: CWD YouTube Channel | |
Version: 0.1 | |
Plugin URI: http://www.celticwebdesign.net | |
Description: Render YouTube video count | |
Author: Darren Stevens | |
Author URI: http://www.celticwebdesign.net | |
*/ | |
function cwd_youtube_view_count_channel_shortcode($params) { | |
$channelID = $params['id']; | |
$json = file_get_contents("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=".$channelID."&maxResults=50&key=XXX"); | |
$jsonData = json_decode($json); | |
$totalVideoCount = 0; | |
foreach ($jsonData->items as $item) { | |
if ( "youtube#video" == $item->id->kind ) { | |
$jsonVideo = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=".$item->id->videoId."&key=XXX"); | |
$jsonDataData = json_decode($jsonVideo); | |
$jsonDataData__count = (int)$jsonDataData->items[0]->statistics->viewCount; | |
$totalVideoCount = $totalVideoCount + $jsonDataData__count; | |
} | |
} | |
return $totalVideoCount; | |
} | |
add_shortcode('cwd_youtube_view_count_channel', 'cwd_youtube_view_count_channel_shortcode'); | |
// [cwd_youtube_view_count_channel id="UCZGwnt2vCa4knhXpXKAi7ZQ"] | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment