Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Last active July 17, 2017 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnymaceachern/990a04f442cacb3fb55d to your computer and use it in GitHub Desktop.
Save jonnymaceachern/990a04f442cacb3fb55d to your computer and use it in GitHub Desktop.
Remove related videos from YouTube embeds
// Remove related videos from youtube embeds
add_filter('embed_oembed_html', 'remove_related_videos_from_yt_oembed', 10, 3);
function remove_related_videos_from_yt_oembed($html, $url, $args = array()) {
if(!stristr($html, 'youtube.com/embed/'))
return $html;
preg_match('/src="(.+?(?="))/', $html, $matches);
if(count($matches) >= 2 && stristr($matches[1], 'youtube.com/embed/'))
{
$realUrl = $matches[1]; // the $url param is the ORIGINAL url, not the src url
$newUrl = $realUrl.(stristr($realUrl, '?') ? '&' : '?').'rel=0';
$html = str_replace($realUrl, $newUrl, $html);
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment