Hi, my name is
Lee Harris and
I'm a Freelance Web Designer

WordPress: Filter a few parameters into YouTube oEmbed requests

22.May.2021 | ,

A quick snipped of code filter parameters into YouTube oEmbed requests.

Handy to remove suggested videos, YouTube contols and info. Paste this into your functions.php file

function 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', 'custom_youtube_querystring', 10, 4);

, , ,

  • html5
  • css3
  • sass
  • php
  • WordPress
  • WooCommerce