When you add a YouTube video link to your WordPress page or post it is automatically converted to valid html code when it is viewed. At the end of video you will be shown a list of related videos to encourage you to watch more.
I help out on the web site for my sons’ primary school, Castleknock Educate Together National School. They sometimes include YouTube videos in their posts. I felt that the display of related videos could lead the viewers, which may include children, to possibly inappropriate videos, so I looked at a way to automatically disable the related videos feature.
Including a YouTube video in WordPress is very simple – just paste the video link into the post.
and it is automatically converted to:
When you go to YouTube.com and generate the html code to embed a video on your web site you will see the option to “Show suggested videos when the video finishes“. Unchecking this appends rel=0 to the video url.
The code below implements the “unchecking” dynamically. It searches the generated html for ‘rel=1‘ (added by YouTube by default) and changes it to ‘rel=0‘.
Note: I originally wrote this code back in May 2014 when automatic oEmbed was relatively new to WordPress.