wp_editor_set_quality │ filter-hook │ WP 3.5.0

Allows changing the default image compression quality based on its MIME type and dimensions.

The image editor applies it when WP_Image_Editor::set_quality() is initialized or called without specifying a quality. A value explicitly passed to the method takes precedence over the filter.

During client-side image processing, WordPress also applies the filter separately to each generated size and passes the resulting quality to the browser.

For JPEG, jpeg_quality is applied after this filter and can change the resulting value.

Recommended image quality
Format Regular High
JPEG 80-85 88-92
WebP 75-80 82-88
AVIF 50-60 60-70
HEIC 55-65 65-75
PNG lossless lossless

Optimal values for most sites: JPEG — 82, WebP — 78, AVIF — 55.

Quality values for different formats cannot be compared directly. For example, AVIF at quality 55 can look like JPEG at quality 82 while taking up less space. For PNG, the quality parameter usually controls compression level rather than visual image quality.

Usage

add_filter( 'wp_editor_set_quality', 'wp_kama_editor_set_quality_filter', 10, 3 );

/**
 * Function for `wp_editor_set_quality` filter-hook.
 * 
 * @param int    $quality   Quality level between 1 (low) and 100 (high).
 * @param string $mime_type Image mime type.
 * @param array  $size      Dimensions of the image.
 *
 * @return int
 */
function wp_kama_editor_set_quality_filter( $quality, $mime_type, $size ){
	// filter...
	return $quality;
}
$quality(int)
Default compression quality from 1 — minimum to 100 — maximum.
$mime_type(string)
MIME type of the output image. For example, image/jpeg, image/webp, or image/avif.
$size(array)

Output image dimensions.

  • width(int)
    Width in pixels.

  • height(int)
    Height in pixels.

Examples

#1 Reduce quality for small JPEG images

Sets quality to 60 for JPEG images narrower than 300 pixels.

add_filter( 'wp_editor_set_quality', 'kama_set_small_jpeg_quality', 10, 3 );

function kama_set_small_jpeg_quality( $quality, $mime_type, $size ) {
	if (
		'image/jpeg' === $mime_type
		&& isset( $size['width'] )
		&& $size['width'] <= 300
	) {
		return 60;
	}

	return $quality;
}

#2 Different quality for each format

Sets a separate quality for JPEG, WebP, and AVIF.

add_filter( 'wp_editor_set_quality', 'kama_set_image_quality_by_format', 10, 3 );

function kama_set_image_quality_by_format( $quality, $mime_type, $size ) {
	$quality_by_format = [
		'image/jpeg' => 82,
		'image/webp' => 78,
		'image/avif' => 55,
	];

	return $quality_by_format[ $mime_type ] ?? $quality;
}

PNG and GIF use lossless compression. Their filter value does not control image quality consistently across all WordPress image editors, so setting a separate value for them is unreliable.

Changelog

Since 3.5.0 Introduced.
Since 6.8.0 Added the $size parameter.

Where the hook is called

WP_Image_Editor::set_quality()
wp_editor_set_quality
wp_get_image_encode_quality()
wp_editor_set_quality
wp-includes/class-wp-image-editor.php 269
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
wp-includes/media.php 1013
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $size );

Where the hook is used in WordPress

Usage not found.