and like us on FB

This is the first of a couple of a few articles that’ll deal with obtaining data from YouTube’s API (version 3). This post will show you how to use YouTube API to render a viewcount (of total videos), subscriber count, and video count in your WordPress post or page with shortcode, or in a PHP application. A function that’ll render data related to a specific video is scheduled in an another article.

Usage of the YouTube API requires an API Key .

In company with our custom text boxes, you can render a result that looks a little like the one below.

Join 21,979 subscribers that have watched our 111 YouTube videos over 9,117,092 times. Subscribe to our YouTube channel here .

WordPress Shortcode Function

We’ve include two WP shortcode functions; one that utilities the transient API, and other that uses Simple Cache. The former won’t return a result if the expiry time has passed and the API can’t be reached. Simple Cache, however, will return old data when the API does not respond. We could use a non-expiring option to store the data with the transient to simply record the expiry, but it’s added complexity that isn’t required.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

The second function utilizes Simple Cache to cache the resulting data array. Use the above function or the one below – not both. Simple Cache may be installed as a standalone plug-and-play plugin.

The shortname name is adjusted for the sake of conflict, but usage is identical. [ytchannel_sc type="views"] will return 9,117,092.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Usage

Shortcode usage is as follows: [ytchannel_sc type="views"] (total video views), [ytchannel_sc type="count"] (total video count), or [ytchannel_sc type="subscribers"] (total subscribers). Available attributes are listed below.

Shortcode Attributes

apikey

The API key is obtained via your Google developer console .

channel

The channel is your channel ID, not your alias. For example, our custom channel URL for FLIGHT is youtube.com/flightorg but our channel URL is https://www.youtube.com/channel/UCQq1yfBzyvRFGDs2TjmZTZw. The latter part of the URL (or everything after channel/) is your ID. You can find your ID by selecting My Channel from YouTube’s left hand menu.

Retrieve Total YouTube Channel Views, Subscribers, and Video Count (with PHP or WordPress Shortcode)

n

n determines whether your results is returned in number format (defaults to true). Alternate notations can be created if desired.

type

The type is either views (total video views), subscribers (total subscribers), or count (total number of videos).

cache

cache determines the length of time the result is cached locally.

PHP Function

The PHP function will query the YouTube API and cache the resulting array locally for a defined period. As such, use of Simple Cache is required.

Usage is as follows:

WordPress Plugin

If you’re using WordPress and prefer the convenience of a plugin, you can download one below. It includes administration options that define the default value to return with [ytchannel]. Any attributes provided in your shortcode will overwrite the defaults.

Retrieve Total YouTube Channel Views, Subscribers, and Video Count (with PHP or WordPress Shortcode)

UPload and activate via your plugins menu. Select Tools -> BeliefMedia YouTube Channel Data to define the defaults (and API key).

Considerations

  • A function (and plugin) to render individual YouTube video stats is scheduled here.

Download

Title: YouTube Channel Views, Subscribers, and Video Count
Description: Render YouTube video count, channel subscribers, or total video views into WP with shortcode.
Download: Shortcode (V0.2) | WP Plugin (V0.2) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/2hNu