Home Blog Display YouTube Videos, Video Views and other Data with PHP or WordPress...

Display YouTube Videos, Video Views and other Data with PHP or WordPress Shortcode

0

This article provides the necessary code to render YouTube video views, titles, descriptions, likes, dislikes, and other data into your WordPress website or PHP application with shortcode. The functions require that you register for an API key via Google’s Developer Console .

Both the WordPress and PHP function share a single function that makes the request for video data and then caches the result. Since we cache all information returned by YouTube, we require installation of Simple Cache (available as WP plugin) so repeated requests aren’t made for the same data. Using Simple Cache also permits you to style various results without multiple requests, and the data is also available from your other applications and websites. We do, however, cache the styled container we build as a WP transient.

Sample Result

A simple example of returned data in a styled container is as follows. We’ve used our very dodgy blind shortcode for the share link. It’s expected that the container will be rendered below a video.

Boeing 777 Engine Failure during Takeoff
4,710,405 views

Published on September 14, 2012. Demonstration of an engine failure during the takeoff roll in a Boeing 777. This video was produced for the CRM/NTS training environment so, as such, doesn't necessarily demonstrate or reflect best operating or SOP compliant practices. These videos were produced for exclusive use by BeliefMedia.
734 5,322 1,206

[ Share ]

The container we’ve built takes up a little real-estate, so you may want to include all the information in a blind as shown below. We’ve included the Simple Cache video for the purpose of the demo.

[ Video Information & Share ]

PHP Function

The following PHP function makes the request for YouTube data and caches the result as a serialized array with Simple Cache.

With the video array cached, you simply need to retrieve the resulting array via beliefmedia_get_transient($transient, $cache) and format the results to your liking.

WordPress Shortcode

The shortcode example is minimal. Usage is as follows: [youtube_video v="C7O7R_Vk7Ko"].

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

The result:

Boeing 777 Engine Failure during Takeoff
4,710,405 views

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.

Return Video Views, Thumbnails, and Other Information

There are times when you will need to return specific pieces of video information. For example, the shortcode of [ytdata v="C7O7R_Vk7Ko" type="views"] returns 4,710,405. You could optionally return the number or likes, dislikes, comments, or the publish date, title, or description. Each piece of data is returned by altering the type attribute in the shortcode. For example, [ytdata v="C7O7R_Vk7Ko" type="published"] returns September 14, 2012. You may optionally return a full constructed thumbnail image. For example, [ytdata v="C7O7R_Vk7Ko" type="thumbnail_high"] returns:

Boeing 777 Engine Failure during Takeoff

The black bars can be removed with a little post processing but it’s beyond the scope of our simple function. Alter the code if you’d like the image to link to the video.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

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.

The function requires beliefmedia_youtube_video_data() and Simple Cache.

Shortcode Attributes are as follows:

v

The video ID.

type

The type of data to be returned. Options are: views, likes, dislikes, comments, published, title, description, thumbnail_small (120 x 90), thumbnail_medium (320 x 180), thumbnail_high (480 x 360), thumbnail_standard (640 x 480), thumbnail_maxres (1280 x 720).

date

The date format. Formatting can be constructed by way of information in the PHP manual .

cache

The time to cache the result.

An advantage of using Simple Cache is that YouTube data is cached for a period as defined by you. Requests for array values won’t make unnecessary requests to YouTube after the first request is made. When a new request is made for data (as defined by the cache period in Simple Cache) and the API request doesn’t return a response, old data will instead be recycled.

Considerations

  • A sample array returned by YouTube is published here. To view the data returned via your own request, use the following wrapped in pre tags:

  • In our more complete example we snip the description by sentence length. To snip the description by length we have a number of options here. A function to return only a certain number of sentences is as follows:

    Rather than loop through the array as we’ve done, you might consider using array_slice() .

  • Our featured meta box includes share icons for just a few websites. We used a function similar to the one below. To add additional icons, simply add them to the list. While we add the YouTube URL as the link, you may want to alter this in WordPress to your post permalink.

  • Find other YouTube related posts via our YouTube Tag: youtube tag.
  • See also: “Retrieve Total YouTube Channel Views, Subscribers, and Video Count (with PHP or WordPress Shortcode)” here. The function returns channel data that’ll allow you to return data as follows:

Join 25,791 subscribers that have watched our 111 YouTube videos over 11,077,181 times. Subscribe to our YouTube channel here .

  • Alter all occurrences of images hosted by us to your own website.

Download

Title: Display YouTube video data with WordPress or PHP
Description: Render YouTube video views, titles, descriptions, likes, dislikes, and other data into your WordPress website or PHP application with shortcode.
Download: Shortcode (V0.2) | PHP Code (V0.2) | Plugin Page

Shortt URL for this post: http://shor.tt/17N0