Limit YouTube Related Videos to Same Channel

Written by Dan Mossop

The Divi Theme allows you to embed YouTube clips in your site in several ways, such as by pasting the URL within a text module, or by adding the URL in a video module.

Once the user has played the embedded video, YouTube presents them with a selection of other videos. These videos may contain videos you'd prefer not to have your user be presented with, such as videos by competitors or irrelevant videos that will distract them from your site.

YouTube used to provide an option to disable these related videos entirely by appending rel=0 to the video URL. They have now changed the behavior of this option to still show related videos, but only from the same channel as the embedded video. This means if the video is from your own channel, the user will be shown a selection of your own videos rather than those from other channels.

Unfortunately there is no built-in way to add the rel=0 parameter in Divi – if you include it in your embedded link it is simply stripped out (due in part to WordPress's own handling of embedded links).

There are several options for persuading Divi to restrict the related videos to the current channel.

Limit YouTube Related Videos to the Same Channel in the Video Module

As of version 2.8.3, Divi Booster adds an option to the video module allowing you to simply toggle a button to limit related videos to the current channel.

You'll find the option in the Video Module Settings under "Content > Video > Limit YouTube related videos to same channel". Set this to "Yes" and it'll restrict the related videos to the same channel as the current video.

As of Divi Booster v4.5.0, the rel parameter is now added to playlists, restricting their related videos to your own channel too. 

Limit YouTube Related Videos to the Same Channel Site-Wide

You can use the following PHP code to add the rel=0 option to any YouTube videos embedded in your site:

add_action('the_content', 'dbvideo_html_without_youtube_related_videos', 100);

if (!function_exists('dbvideo_html_without_youtube_related_videos')) {
    function dbvideo_html_without_youtube_related_videos($old_content) {
        $new_content = preg_replace_callback("/https?:\/\/www\.youtube\.com\/[^\"]*/i", 'dbvideo_url_without_youtube_related_videos', $old_content);
        return apply_filters('dbvideo_html_without_youtube_related_videos', $new_content, $old_content);
    }
}

if (!function_exists('dbvideo_url_without_youtube_related_videos')) {
	function dbvideo_url_without_youtube_related_videos($match) {
		$old_url = isset($match[0])?$match[0]:'';
		$new_url = add_query_arg('rel', '0', $old_url);
		return apply_filters('dbvideo_url_without_youtube_related_videos', $new_url, $match);
	}
}

Optimize Your YouTube Embeds with Divi Booster

Ensure your site only shows videos from your channel by using Divi Booster. With this plugin, you can easily toggle the option to limit YouTube related videos to your own channel within the Divi video module. Keep your audience engaged with your content, not competitors'.

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

28 Comments

  1. The PHP into my child theme's functions.php was PERFECT, thank you very much!

    Reply
    • Always a pleasure, Roger. Thanks!

      Reply
    • Hey John, thanks for bringing this to my attention.

      I've think I've identified the cause, and the next update to Divi Booster (v4.3.2) will include a fix for it.

      I'm aiming to release this early next week and will update here when it's ready. Thanks!

      Reply
      • Thanks very much Dan. Always a pleasure.

        Reply
        • Likewise, John. I've just released the update (v4.3.2) – hopefully it'll solve the issue for you, but let me know if not. Cheers!

          Reply
          • Thanks Dan. It seems to have been working properly of late, although a bit inconsistently. On the video on our loans page https://ncfdc.ca/loans/ it works like a charm going back to the beginning of the video which is ideal. On our home page it lists other videos from the same channel as expected.

            We tried using it on a list at https://ncfdc.ca/venture13/ and it doesn't seem to work at all. Is it supposed to work on lists?

          • Hey John, the pattern I was using to match the youtube urls was specific to individual video urls and so wasn't applying to playlists. I've update the code so that the rel parameter gets added to all youtube urls, including playlists. This fix will also be in the next version of Divi Booster (v4.5.0), which I'll hopefully be able to release early next week. I hope that helps!

          • Hey John, sorry it took a bit longer than planned to get this released. The update is now available so it should now work on playlists too. Give me a shout if you have any questions about it. Cheers!

    • Hey Tyler, thanks for the link. I'm sure you know this already but, just for the benefit of everyone else, the link relates to an update in Drupal. For those of us running WordPress (as anyone using Divi will be), the link / patch it discusses won't apply. But certainly a useful update for Drupal users, and thanks again for sharing :)

      Reply
  2. This snippet worked great for me, thanks!

    Reply
    • Hi trickynek, it looks like the code was failing to apply when the YouTube URL contains a hyphen or underscore. I've updated the post with a fix, and this will be in the next version of Divi Booster (2.9.1). I hope that helps!

      Reply
      • Thank you!!

        Reply
        • You're welcome, trickynek!

          Reply
  3. Hi Dan – wanted to say thanks for posting this – your PHP snippet worked fantastically when I first implemented and solved the issue, but for some reason it no longer works.

    We have it in functions.php and I witnessed it working brilliantly, but now the output iframe looks like this:

    No rel=0 to be seen!

    Have YouTube changed something?

    Cheers, Sam

    Reply
    • You're welcome, Sam. Are you able to send me either a link to the page with the video, or a copy of the youtube link as you see it in the iframe? I don't think this will be caused by a change in youtube, but it might be caused by some change in the format of the link being displayed by Divi. My code is looking for a specific link format within the page, so if there is some difference in the format of your link (or Divi's rendering of it) that I haven't taken into account, then the code would fail to add the rel=0 parameter. Cheers!

      Reply
        • Hey Sam, I set up a test on my own site embedding the same video using ACF's oEmbed feature and placing the acf shortcode in a code module (which seems to be the same basic set up you have, right?). This produced exactly the same HTML for the iframe as on your site and when I added the code in the post above it added the rel=0 parameter correctly. I also grabbed the HTML source of your page and passed it through the disable_related_youtube_videos() function – again the rel=0 parameter was added. So it doesn't seem to be a problem with the code itself. It sounds more like the code isn't being run at all. There are a couple of things I'd try. First, check in functions.php to see if you can see any reason the code wouldn't be running. It might be worth moving the code to the very top of the file to rule out any unclosed comments, etc, further down the file. Next, try adding the link to a regular post instead of the custom post type you're using, to see if that works. I see you're using CPT Layout Injector and I'm wondering if it might be conflicting in some way. I also suggest increasing the "100" in the last line of the code (to say "10000"). This will delay running the code and might bypass any other plugins, etc, that are adding code using the same action hook. Hopefully one of these helps, but let me know if not. Thanks!

          Reply
  4. is this option no longer available, I can't find it anymore as an option :(

    Reply
    • Hey Linda, the option should still be available… I've checked on my test sites and it is still working there, but I've just had another comment reporting the same problem so clearly something is going on.

      The option should be in the Video Module settings on the "Content" tab. On that tab, you should see the "Video" subheading and find the option in question just after the "Video Webm" option. You should also be able to find it by searching the Video Module settings for "youtube".

      Assuming you still don't see it, is there any chance you'd be able to share a link to the site you're working on and, if at all possible, login details so that I can investigate further? If you are able to send login details, you can do so via the contact form.

      Thanks!

      Reply
  5. Hey, I have Divi Booster 2.8.3, but I am not finding this option in the module. Is there any reason you can think of why I am not seeing it?

    Reply
    • Hey Ralph, I'm not quite sure why the option isn't showing up for you… I've checked on my test sites and it is still working there, but I've just had another comment reporting the same problem so clearly something is going on.

      The option should be in the Video Module settings on the "Content" tab. On that tab, you should see the "Video" subheading and find the option in question just after the "Video Webm" option. You should also be able to find it by searching the Video Module settings for "youtube".

      Assuming you still don't see it, is there any chance you'd be able to share a link to the site you're working on and, if at all possible, login details so that I can investigate further? If you are able to send login details, you can do so via the contact form.

      Thanks!

      Reply
  6. Unfortunately this doesn't work for me :-( At the end of my video, a lot of videos from other channels are displayed.

    Reply
    • Hi CorinnaK, sorry I'm only just responding now. If you're still having this issue, is there any chance you're able to share a link to the page you're working on so that I can take a look at what's going on? Thanks!

      Reply
  7. This is perfect! I have several clients wanting to limit the videos to their own, but the old way wasn't working. Thanks.

    Reply
    • You're welcome, Shanora :)

      Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.

We may earn a commission when you visit links on our website.