Save JPEG and PNG Uploads as WebP
Makes WordPress generate WebP versions instead of JPEG and PNG for every image size it creates on upload, which usually cuts image weight by a quarter or more.
Opens WPIDE on
Code
// Output format for resized images: image/webp or image/avif.
$wpide_output_format = 'image/webp';
add_filter( 'image_editor_output_format', function ( $formats ) use ( $wpide_output_format ) {
static $checking = false;
if ( $checking ) {
return $formats;
}
$checking = true;
$supported = wp_image_editor_supports( array( 'mime_type' => $wpide_output_format ) );
$checking = false;
if ( $supported ) {
$formats['image/jpeg'] = $wpide_output_format;
$formats['image/png'] = $wpide_output_format;
}
return $formats;
} );
Notes
Needs a server whose GD or Imagick supports WebP; otherwise nothing changes. The original upload stays in its first format. Set the format to image/avif for AVIF.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.