We'll create fresh WordPress site with Related YouTube Videos installed. You have 20 minutes to test the plugin after that site we'll be deleted.
Related YouTube Videos is a free WordPress plugin that embeds a number of, well, YouTube videos that are related to your content. The list is put together by using the YouTube search API. And you can specify the relation between the videos and your content by:
Also, this plugin offers you two ways to embed related videos:
As of April 2015 the YouTube API can only be accessed when you have a valid key for the data API. Also, the number of free requests per day is limited!
There is a nice and short video tutorial (https://www.youtube.com/watch?v=zOYW7FO9rzA) on how to get such an API key. But it’s up to you to make sure your stay within the free limit or keep track of the costs when you need more than that!
Please read Youtube and Google’s terms and conditions for more details.
I’m only going to explain the options/paramters for the shortcode but they work exactly the same for the widget, of course.
You can put the shortcode [relatedYouTubeVideos] anywhere you want inside the content of a page, post, or custom post type – as long as your WordPress theme is supporting shortcodes.
You can use the following options/parameters/attributes:
Appearance
<ul>
element.<ul>
element.title
.description
Configuration
YouTube
I recommend always using the attributes ‘relation’, ‘max’, and if the relation shall be ‘keywords’ the ‘terms’ attribute. Depending on your design you might also set a custom width and height for the videos so they fit in properly.
Shortcode Example 1: [relatedYouTubeVideos relation=”postTags” max=”3″] Will show three videos coming back from the search YouTube for (all!) the tags you have assigned to this post or page.
Shortcode Example 2: [relatedYouTubeVideos relation=”keywords” terms=”monty python” max=”5″] Will show five Monty Python videos from YouTube.
Shortcode Example 3: [relatedYouTubeVideos relation=”keywords” terms=”real madrid” exact=”true” max=”2″] Will search for the exact phrase “Real Madrid” and (hopefully) not just anything “real”.
Shortcode Example 4: [relatedYouTubeVideos relation=”postTitle” max=”1″ orderBy=”viewCount” start=”1″] Will show the second most popular video (the first being skipped) relating to your post or page title.
Shortcode Example 5: [relatedYouTubeVideos relation=”keywords” terms=”monthy python” max=”1″ showVideoTitle=”true” showVideoDescription=”true”] Will show a Monty Python video, followed by the video title, followed by the video, followed by the video description.
Shortcode Example 6: [relatedYouTubeVideos relation=”keywords” terms=”monthy python” max=”1″ preview=”true”] Will show the thumbnail of a Monty Python video and load + play the video only when it’s being clicked.
Shortcode Example 7: [relatedYouTubeVideos relation=”postTitle” filter=”intitle:official -intitle:cover” max=”1″ preview=”true”] If the post title is about a music video (band and song title, for example) this will only show the official music video.
Shortcode Example 8: [relatedYouTubeVideos relation=”postTitle” filter=”+postCategories” max=”1″ preview=”true”] Will use the post title and a list of all categories the post is in to search YT.
The widget almost works the same way. Or at least it has the same options for configuring the video request. If you log into your WordPress backend and go to the “Appearance > Widget” menu (given that your theme supports widgets) you can drag&drop a widget instance into the widget area of your choice.
The relatedYouTubeVideos widget allows multiple instances. So you can put as many widgets as you like into as many widget areas as you like.
The difference between the widget and the shortcode is not in terms of functionality but usually in the context they reside. Widgets usually go into sidebars or footers and alike and the same widget usually shows up for many, if not all pages, just the same. Shortcodes are placed inside the actual content and therefore will only show up when the page or post where they’re put is shown.
There is one functionality that only applies to widgets: In the options field “Hide on” you can set a list of comma separated, numerical post IDs on which the widget will not show up. This way you can easily exlude the widget on certain post. You can also use PAGE ids for that matter, since under the hood WordPress manages all “post type” IDs the same.
The same keywords (or post title/tags) will ususally return the same video(s) for a period of time. Basically, that’s up to YouTube but in can take days, weeks or even months until fresh videos will show up.
When you set a numeric value for the RANDOM parameter/option you can get random videos from a pool of results. The MAX value plays along with the RANDOM value and both read like this: Show me {MAX} random videos out of {RANDOM} videos.
[relatedYouTubeVideos relation=”keywords” terms=”fast cars” max=”2″ random=”10″] will actually request 10 videos from YouTube but only show 2 random ones out of that 10.
So RANDOM will determine the size of the pool MAX videos will be chosen from.
In order to run this plugin you need the following components installed and enabled in your server and PHP environment:
In general you should not have to worry about these things since they’re included in most web hosting packages nowadays.
But to be sure you can download and install this plugin and then check the backend page. There is a “System Requirements” section that will show you exactly if you can good to go or if there is any problem.
In case you’re getting a “URL file-access is disabled in the server configuration” error you should make sure your PHP.ini file contains these two lines:
allow_url_include = on
allow_url_fopen = on
Serbo-Croatian – by Borisa Djuraskovic (Webhostinghub)
Developers can also use the API class outside the plugin context, for example in a theme template file. All you have to do is include the class (if it doesn’t already exist) and create an object like this:
/* Load the “Related YouTube Videos” API class if it does not exist yet. */
if( !class_exists( ‘RelatedYouTubeVideos_API’ ) ) {
$file = str_replace( ‘/’, DIRECTORY_SEPARATOR, ABSPATH ) . ‘lib’ . DIRECTORY_SEPARATOR . ‘RelatedYouTubeVidoes’ . DIRECTORY_SEPARATOR . ‘API.php’;
if( file_exists( $file ) ) {
include_once $file;
}
}
/* Only continue if the API class could be loaded properly. */
if( class_exists( ‘RelatedYouTubeVideos_API’ ) ) {
$RytvAPI = new RelatedYouTubeVideos_API();
/* Do your configuration */
$data = $RytvAPI->validateConfiguration(
array(
‘relation’ => ‘postTitle’,
‘max’ => ‘3’,
‘width’ => 150,
‘height’ => 150,
‘lang’ => ‘en’,
‘region’ => ‘de’,
‘class’ => ‘left center inline bg-black’,
‘preview’ => true
)
);
/* Search YouTube. */
$results = $RytvAPI->searchYouTube( $data );
/* Generate the unordered HTML list of videos according to the YouTube results and your configuration. */
$html = $RytvAPI->displayResults( $results, $data );
echo $html; // Or do with it whatever you like 😉
}