Halloween Sale 15% off Ends in See the plans
PHP

Lower Quality for Small Thumbnails, Higher for Large Images

Saves small thumbnails with stronger compression and large images with higher quality, so grids load fast while hero images stay crisp, instead of one quality for everything.

Don’t have WPIDE yet? Install it on your site

Code

// Width threshold and the two quality levels.
$wpide_small_size_px = 400;
$wpide_small_quality = 70;
$wpide_large_quality = 88;

add_filter( 'wp_editor_set_quality', function ( $quality, $mime_type, $size = array() ) use ( $wpide_small_size_px, $wpide_small_quality, $wpide_large_quality ) {
    if ( ! in_array( $mime_type, array( 'image/jpeg', 'image/webp' ), true ) || empty( $size['width'] ) ) {
        return $quality;
    }

    return (int) $size['width'] <= $wpide_small_size_px ? $wpide_small_quality : $wpide_large_quality;
}, 10, 3 );

Notes

Needs WordPress 6.8+, which tells the quality filter which size is being made; applies to JPEG and WebP images created from now on.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.