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.
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 0.
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
channel
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.n
type
cache
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.
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 (Shortcode)
Description: Render YouTube video count, channel subscribers, or total video views into WP with shortcode.
Download • Version 0.2, 1.1K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (996.0B) WordPress Plugins (General), (3.4K)