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.
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.
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:
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 0. 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 January 1, 1970. You may optionally return a full constructed thumbnail image. For example, [ytdata v="C7O7R_Vk7Ko" type="thumbnail_high"]
returns:
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
type
date
cache
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:
- 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:
1<?php2/*3Display YouTube Videos, Video Views and other Data with PHP or WordPress Shortcode4http://www.beliefmedia.com/youtube-video-data5Function returns a number of sentences6*/78910/* Remove breaks */111213/* How many sentences in the text? */14151617$i = 0;18while ($i <= $sentences) {1920$i++;21}222324}252627}
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.
1<?php2/*3Display YouTube Videos, Video Views and other Data with PHP or WordPress Shortcode4http://www.beliefmedia.com/youtube-video-data5Returns share icons and links6*/78910$return = '<a href="https://twitter.com/intent/tweet?url=' . urlencode('https://youtu.be/' . $v) . '&text=' . urlencode($title) . '&via=' . $twitter_via . '&related=' . $twitter_related . '"><img src="wp-images/site/yt-icons/twitter.jpg"></a>';11$return .= ' <a href="https://www.facebook.com/sharer/sharer.php?kid_directed_site=0&u=' . urlencode('https://youtu.be/' . $v) . '"><img src="wp-images/site/yt-icons/facebook.jpg"></a>';12$return .= ' <a href="https://plus.google.com/share?url=' . urlencode('https://youtu.be/' . $v) . '"><img src="wp-images/site/yt-icons/googleplus.jpg"></a>';13$return .= ' <a href="https://www.blogger.com/blog-this.g?n=' . str_replace(' ', '+', $title) . '&eurl=https://i.ytimg.com/vi/' . $v . '/maxresdefault.jpg&b=%3Ciframe+width%3D%22480%22+height%3D%22270%22+src%3D%22https://www.youtube.com/embed/' . $v . '%22+frameborder%3D%220%22+allowfullscreen%3E%3C/iframe%3E"><img src="wp-images/site/yt-icons/blogger.jpg"></a>';14$return .= '<input type=text style="width: 200px; font-size: 12pt; height: 25px;" value = "http://youtu.be/' . $v . '" onClick="this.select();">';1516return $return;17}
- Find other YouTube related posts via our 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:
- Alter all occurrences of images hosted by us to your own website.
Download
Title: Display YouTube Video Data in WordPress (WP Shortcode)
Description: Render YouTube video views, titles, descriptions, likes, dislikes, and other data into your WordPress website or PHP application with shortcode.
Download • Version 0.2, 2.5K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (1.4K)