SOLVED: YT Video Improvement: Change default API preview image quality

Hi,

Is there a possibility to change the default YouTube API preview image quality? Something like WP Rocket has: Choose YouTube preview image resolution - WP Rocket Knowledge Base

Thanks

3 Likes

UPDATE

I’ve checked the video element code and here is the part used for chosing the video thumbnail:

// STEP: Get YouTube video preview image from API
if ( $video_type === 'youtube' ) {
     return "https://img.youtube.com/vi/{$settings['youTubeId']}/hqdefault.jpg";
}

So, there is no way to change the thumbnail quality except editing the code directly.

It would be really handy to add there a possibility to change the quality. At least via filter, ideally in UI.

The simple filter could look something like this:

// STEP: Get YouTube video preview image from API
if ( $video_type === 'youtube' ) {
    $thumbnail_resolution = apply_filters( 'bricks/youtube_thumbnail_resolution', 'hqdefault' );
    return "https://img.youtube.com/vi/{$settings['youTubeId']}/{$thumbnail_resolution}.jpg";
}

And yes, I’m aware this could lead to possible issues, since some of the resolutions might not be available. If using the UI, there should be some info about it there.

I’m making this a feature request now. Thanks for considering it…

My client was very unhappy when the Preview image by Default API is completely pixelated in full width container… Bricks uses a 480x360 hqdefault resolution, which is good for mobile devices, but definitely not for desktops. For 1280x720, you just need to change: return ā€œhttps://img.youtube.com/vi/{$settings[ā€˜youTubeId’]}/hqdefault.jpgā€;
in to:
return ā€œhttps://img.youtube.com/vi/{$settings[ā€˜youTubeId’]}/maxresdefault.jpgā€;

@thomas @Matej I think it’s not too hard to provide a filter or option?

My solution, but I need confirmation if this is a good approach?:

/**

  • Replace YouTube hqdefault thumbnails with maxresdefault in rendered output
  • Add this to your child theme’s functions.php
    */
    add_filter( ā€˜bricks/frontend/render_data’, ā€˜replace_youtube_thumbnail_quality’, 10, 3 );

function replace_youtube_thumbnail_quality( $content, $post, $area ) {
// Only process if content contains YouTube thumbnails
if ( strpos( $content, ā€˜img.youtube.com/vi/’ ) === false ) {
return $content;
}

// Replace hqdefault.jpg with maxresdefault.jpg
$content = str_replace(
    '/hqdefault.jpg',
    '/maxresdefault.jpg',
    $content
);

return $content;

}

Hi @drigis3,

I have not testes your workaround, but if it’s working, I don’t see any other problem.

Also, I’ve created an internal improvement task for this and linked this topic to it.

Thank you,
Matej

1 Like

We added this imrpovement in Bricks 2.1.3, now available as a one-click update in your WordPress Dashboard.

Please take your time to read the changelog entry before updating, and let us know if you continue to experience issues.