Last active
July 17, 2017 17:05
-
-
Save jonnymaceachern/990a04f442cacb3fb55d to your computer and use it in GitHub Desktop.
Remove related videos from YouTube embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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