When you embed a video into WordPress by pasting the URL into the editor, when the clip reaches the end, YouTube will display related videos. By default, YouTube will display thumbnails for any videos it deems “related”. Sometimes, YouTube will display videos from competitors.
In any event, you don’t want visitors distracted by content from outside sources. Here is how you prevent YouTube from displaying unrelated videos.
The Old Solution
At one time, you could instruct YouTube to not display any related videos by adding a “rel=0” parameter to the URL. YouTube would run the video to the end and then stop, which was perfect.
YouTube No Longer Permits Hiding Related Videos
In September 2018, YouTube changed the rules. Now, when “rel=0” is provided as a URL parameter, they will display related videos — but, they will restrict the list videos from the same channel.
While not optimal, this is a huge improvement over having visitors see content from outside sources.
The WordPress Problem
If you embed a YouTube video in WordPress and append “?rel=0” to the URL, it makes no difference. This is because WordPress ignores all URL parameters when creating the code that embeds the video into the page.
For a time the popular solution was to install a free plugin from the WordPress repository named “Hide YouTube Related Videos” by David Hollander at SparkWeb Interactive. However, he recently removed the plugin from the repository.
The Solution
To prevent YouTube from displaying (un)related videos, all you need to do is add this code to your child theme’s functions.php file.
//Stop YouTube (Un)Related Videos function stop_youtube_unrelated_videos($data, $url, $args = array()) { if (strpos($data, "feature=oembed") !== false) { $data = str_replace('feature=oembed', 'rel=0&feature=oembed', $data); } return $data; } add_filter('oembed_result', 'stop_youtube_unrelated_videos', 10, 3);
If you aren’t already using a child theme, don’t make this to your theme’s functions.php file. If you do, your changes will be overwritten when you update your theme. Need help getting started with a child theme? This article is a good resource.
Summary
With just a few lines of code, you can safely and easily embed YouTube videos in your WordPress website and be confident that the only related videos displayed will be your own.
Is there a way of doing this without editing your functions.php file? You never said whether there was on not.
The best way to accomplish this by adding the code to the child theme’s functions.php file. However, with WordPress there is almost at least one more way to accomplish a task. There are plugins that let you insert PHP code into your website without editing functions.php. Examples: Woody Ad Snippets or Insert PHP Code Snippet. I’ve never used any of these, so I can’t attest to how well they work.
I added your code in my website but still not working. I can still see those unwanted related videos list in the end : https://floridapremierrentals.com/properties/the-meeting-place/ Any help ?
Thanks-
Thomas
Thomas, As explained in the article, YouTube no longer allows you to prevent the display of related videos. However, they do allow you to restrict the related videos to videos from your channel. When I looked the link you provided, the related videos displayed were all from the same YouTube channel.