Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filter a few parameters into WordPress YouTube oEmbed requests. Enable modest branding which hides the YouTube logo. Remove the video title and uploader information. Prevent related videos from being shown once the video has played.
<?php
/**
* Filter a few parameters into YouTube oEmbed requests
*
* @link http://goo.gl/yl5D3
*/
function iweb_modest_youtube_player( $html, $url, $args ) {
return str_replace( '?feature=oembed', '?feature=oembed&modestbranding=1&showinfo=0&rel=0', $html );
}
add_filter( 'oembed_result', 'iweb_modest_youtube_player', 10, 3 );
@mafsdisseny

This comment has been minimized.

Copy link

@mafsdisseny mafsdisseny commented May 25, 2015

Useful for me!
Thanks for sharing

@elliottpost

This comment has been minimized.

Copy link

@elliottpost elliottpost commented Mar 13, 2016

Thank you! +1

@apptikka

This comment has been minimized.

Copy link

@apptikka apptikka commented Mar 27, 2016

This hack did not work for my wordpress @ indiahashtag.com

@jayque

This comment has been minimized.

Copy link

@jayque jayque commented Jul 13, 2016

replace oembed_result with embed_oembed_html for this to work properly

@alexstandiford

This comment has been minimized.

Copy link

@alexstandiford alexstandiford commented Dec 23, 2016

Thanks for this. I made a tweak that makes it easier to build the querystring.

<?php
function imp_custom_youtube_querystring( $html, $url, $args ) {
	if(strpos($html, 'youtube')!= FALSE) {
		$args = [
			'rel' => 0,
			'controls' => 0,
			'showinfo' => 0,
			'modestbranding' => 1,
		];
		$params = '?feature=oembed&';
		foreach($args as $arg => $value){
			$params .= $arg;
			$params .= '=';
			$params .= $value;
			$params .= '&';
		}
		$result = str_replace( '?feature=oembed', $params, $html );
	}
	return $result;
}
add_filter('oembed_result', 'imp_custom_youtube_querystring', 10, 3);
?>
@sejas

This comment has been minimized.

Copy link

@sejas sejas commented Apr 26, 2017

If you have installed jetpack, you need to add this filter:

/* $html example value:
<span class="embed-youtube" style="text-align:center; display: block;"><iframe class='youtube-player' type='text/html' width='1220' height='717' src='http://www.youtube.com/embed/ZuYCXwcCNqM?version=3&#038;rel=1&#038;fs=1&#038;autohide=2&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' allowfullscreen='true' style='border:0;'></iframe></span>
*/
function imp_custom_youtube_querystring( $html ) {
	if(strpos($html, 'youtube')!= FALSE) {
		$args = [
			'rel' => 0,
			'controls' => 0,
			'showinfo' => 0,
			'modestbranding' => 1,
		];
		$params = '?version=3&#038;';
		foreach($args as $arg => $value){
			$params .= $arg;
			$params .= '=';
			$params .= $value;
			$params .= '&#038;';
		}
		$result = str_replace( '?version=3', $params, $html );
	}
	return $result;
}
add_filter('video_embed_html', 'imp_custom_youtube_querystring', 10, 3);
@MatRouault

This comment has been minimized.

Copy link

@MatRouault MatRouault commented Jan 4, 2018

So usefull, thank you @mustardBees !

@MatRouault

This comment has been minimized.

Copy link

@MatRouault MatRouault commented Jan 5, 2018

@alexstandiford thanks a lot for your code.

Unfortunately, when we have both a Youtube video and a Vimeo video on the same page, your code prevents the display of Vimeo video... I can't figure out why? Do you have an idea ?

The original code from @mustardBees does not cause any bug, though.

@chrneumann

This comment has been minimized.

Copy link

@chrneumann chrneumann commented Jan 8, 2018

@MatRouault Replace all occurences of $result with $html and it should work.

@bachdall

This comment has been minimized.

Copy link

@bachdall bachdall commented Jul 19, 2018

@MatRouault did you find a soulution where you are able to hide controls on both youtube and vimeo videos?

@Carlos-mb

This comment has been minimized.

Copy link

@Carlos-mb Carlos-mb commented Sep 15, 2018

How is it posible that if I have deleted the code it still appear in page source?

I tried it with a page that has a video embed. It worked.. then I deleted the code in functions.php but the video still applied the same parameters.

How was it possible? (I asked to myself)

I delete de code, deleted the cache folder... I forced an error in functions.php in order to be sure it's being updated and used... I edited the page and save it again. I deleted the video in the page and paste it again...

But the video is still using those arguments.

And it happened just with the video I have had used to test.. the other videos working fine.

I added the code again but changing one argument "controls=1".. but this video still used the other parameter. I tried other videos and they worked fine.

WHY?

I gone to youtube to get other URL for the same video: https://youtu.be instead of https://youtube.com/

I changed it and it worked fine.

Now I have this in the page:

https://youtu.be/JR06ALEyrEU

https://www.youtube.com/watch?v=JR06ALEyrEU

And I get this in the source code of the page:

<iframe width="840" height="630" src="https://www.youtube.com/embed/JR06ALEyrEU?feature=oembed&rel=0&controls=1&showinfo=0&modestbranding=1&" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

--   |

<iframe width="840" height="630" src="https://www.youtube.com/embed/JR06ALEyrEU?feature=oembed&rel=0&controls=0&showinfo=0&modestbranding=1&&modestbranding=1&showinfo=0&rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

I have tried in other pages and If I use https://www.youtube.com/embed/JR06ALEyrEU (the 1st video I tried with) I get the OLD colde, but If I use any other video, I get the new code (with "controls=1")

It's like if wordpress... in the process of creating the video preview, cached it and do not change it. But I deleted the cache folder.

Don't you love f***ing computing ?

@tomjnsn

This comment has been minimized.

Copy link

@tomjnsn tomjnsn commented Nov 20, 2020

In case someone else comes across this and wants to change the settings for youtube embeds in Wordpress, this only works when you're adding a new video to a page not on existing embeds. In addition, this doesn't work with the way YouTube gives share URLs which uses the youtu.be domain name. This is how I altered it:

function imp_custom_youtube_querystring( $html, $url, $attr, $post_id ) {
  if (strpos($html, 'youtube')!= FALSE || strpos($html, 'youtu.be') != FALSE) {
    $args = [
      'rel' => 0,
      'controls' => 0,
      'showinfo' => 0,
      'modestbranding' => 1,
    ];
    $params = '?feature=oembed&';
    foreach ($args as $arg => $value) {
      $params .= $arg;
      $params .= '=';
      $params .= $value;
      $params .= '&';
    }
    $html = str_replace( '?feature=oembed', $params, $html );
  }
  return $html;
}
add_filter('embed_oembed_html', 'imp_custom_youtube_querystring', 10, 4);

One thing you should note about this is that it will run on every embed on every page load rather than have the more performant way that is suggested above. Probably not that big of a deal unless you've got a really high volume site though, in which case you're better off just removing and re-embedding the video and using the oembed_result filter from above instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.