Darek Kay's picture
Darek Kay
Solving web mysteries

Embed YouTube HD videos in WordPress

As of 24.10.2019, YouTube no longer supports functions for setting or retrieving playback quality.

WordPress supports auto-embeds: certain links are automatically converted into embed widgets. Embedding YouTube videos is done by pasting the URL. Unfortunately, the video quality is low by default. It's possible to specify the quality using the vq parameter. Auto-embeds don't support parameters, though. There is a way to manipulate the generated html code by adding parameters to each embedded YouTube video automatically.

Add the following code to functions.php in your theme folder:

/* Play YouTube videos in the best quality available. */
function oembed_hd( $html, $url, $attr, $post_id ) {
  if ( strpos ( $html, 'feature=oembed' ) !== false )
    return str_replace( 'feature=oembed',
      'feature=oembed&rel=0&vq=hd1080', $html );
  else
    return $html;
}
add_filter('embed_oembed_html', 'oembed_hd', 10, 4 );

The parameters explained:

  • rel=0 — Hides related videos.
  • vq=hd1080 — Forces a 1080p quality. Falls back to the best quality if 1080p is not available.
Embed YouTube HD videos in WordPress