Automatically embed YouTube videos that are related to your content.
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 😉
}
Basically, there are three ways to install this or any other WordPress plugin.
Log into the WordPress backend as someone who has the right to install and activate new plugins. Go the “Plugins” section and click the “Add New” button at the top of the page. Enter “relatedYouTubeVideos” into the search field and hit the “Search Plugins” button. Look out for this plugin and click the “install now” link under the plugin name in the first column. After the plugin has been installed automatically you only have to click the “Activate Plugin” link and you’re good to go.
You can also download the current plugin ZIP file, for example from http://www.WordPress.org/extend/plugins/related-youtube-videos/ and upload it yourself.
Just log into the WordPress backend as someone who has the right to install and activate new plugins. Go to the “Plugins” section and click the “Upload” link. Select the ZIP file from your local machine and hit the “Install Now” button. After the plugin has been installed you only have to click the “Activate Plugin” link and you’re good to go.
You can also download the current plugin ZIP file, for example from http://www.WordPress.org/extend/plugins/related-youtube-videos/, and upload it yourself via FTP, for example.
First you have to download the plugin ZIP file, for example from http://www.WordPress.org/extend/plugins/related-youtube-videos/, and extract the archive on your local machine. Then start any FTP client of your choice (e.g. FileZilla) and connect to your web server. Browse to your WordPress’ root directory and then into /wp-content/plugins/. Now upload the /related-youtube-videos/ folder you just extracted from the ZIP archive with all its containing files.
Then log into the WordPress backend as someone who has the right to install and activate new plugins. Go to the “Plugins” section and look for the “relatedYouTubeVideos” in your plugin list. If the plugin doesn’t appear in this list, you might have to switch to the “Inactive” tab and look again. If you have found it just hit the “Activate” link below the plugin name in the first column and you are good to go.
If you have any question, any kind of suggestion, or maybe a feature request, please let me know. All feedback except trolling is welcome!
= 1.9.9
* security fixes: nonce + sanitization
= 1.9.7
* https fix
* random picking fixed
= 1.9.4
* ‘exclusion’ fix
= 1.9.2
* Dropped the former limit of 10 results max to whatever you want (Youtube will cap that, not this plugin)
* Fixed the programmatical approach to not display results on a given list of post IDs (exclude=”1,2,3″ etc)
= 1.9.0
* New widget (only) option “Hide on” (list of comma separated post IDs)
= 1.8.5
* orderBy=”published” issue fixed.