Skip to content

Instantly share code, notes, and snippets.

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