Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Last active August 11, 2021 14:40
  • Star 28 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mustardBees/7704269 to your computer and use it in GitHub Desktop.
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 );
@bachdall
Copy link

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
Copy link

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
Copy link

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