When you paste a YouTube video in the post editor WordPress detects that it’s a YouTube link and expands the content using oEmbed features of YouTube.

 

The problem with the default embedding is that after the video has ended more videos are shown which are supposed to be related. The thing is this may distract the users especially if you’re showing your product’s features. Also you may want to have a minimalistic YouTube branding.

The snippet below appends 2 parameters which enable YouTube’s modest Branding in YouTube videos. modestbranding=1 activates the modest branding.
rel=0 disables the related videos.

To use this functionality you can either append it to your functions.php file or create an mu-plugin (system plugin)

<?php
/**
 * Parses youtube links and appends some parameters to prevent Yt to show extra videos
 * @author Slavi Marinov | https://orbisius.com
 * @see https://orbisius.com/p4075
 */

$obj = new Orb_Quick_Fix_YouTube_Modest_Branding();

add_action('init', array($obj, 'init'));

/**
 * Class Orb_Quick_Fix_YouTube_Modest_Branding
 */
class Orb_Quick_Fix_YouTube_Modest_Branding {
    /**
     *
     */
    public function init() {
        add_filter('the_content', [ $this, 'parse_embed_links' ], 100);
    }

    /**
     * Parses youtube links and appends some parameters to prevent Yt to show extra videos
     * @param $buff
     * @return mixed
     */
    public function parse_embed_links($buff) {
        // No vimeo links so don't do anything. Period.
        // quick efficiency check before doing the regex.
        if (stripos($buff, 'youtube.com') === false) {
            return $buff;
        }
		
		// src="https://www.youtube.com/embed/tXEtv63sTRQ?rel=0&modestbranding=1&feature=oembed"
        $regex = '#(https?://(?:www\.)?youtube\.com/embed/[\w\-]+)(?:\?([\w\=\-\&\%\+]*))?#si';

        // We need to match all the videos on the page.
        if (preg_match_all($regex, $buff, $matches, PREG_SET_ORDER)) {
            foreach ($matches as $match) {
                $embed_src = $match[0]; // &showinfo=0
            	$other_params_query_str = $match[2];
            	// remove any exiting stuff
	            $other_params_query_str = preg_replace('#[&\?]?(rel|modestbranding)=(\d+|true|false)#si', '', $other_params_query_str);
	            $buff = str_replace($embed_src, $match[1] . '?rel=0&modestbranding=1&' . $other_params_query_str, $buff);
            }
        }

        return $buff;
    }
}

 

Is there a plugin?

If you'd rather install a plugin we have that option for you.

Check out https://orbisius.com/products/wordpress-plugins/orbisius-youtube-modest-branding

 

Related

http://leaderswest.com/2012/10/10/youtube-embed-options-can-make-your-videos-look-less-youtube-y/

Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer