Make WordPress save the resized versions of uploaded JPEG and PNG images as WebP, which is usually 25–35% smaller.
add_filter( 'image_editor_output_format', function ( array $formats ): array {
$formats['image/jpeg'] = 'image/webp';
$formats['image/png'] = 'image/webp';
return $formats;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WebP images are noticeably smaller than JPEGs at the same quality, and every modern browser supports them. Since WordPress 5.8 you can ask it to create its image sizes in WebP, with no conversion plugin.
When WordPress creates resized copies of an upload, the image_editor_output_format filter decides their file type. The snippet maps JPEG and PNG to WebP, so thumbnails, medium, large and the scaled full size are saved as .webp.
WordPress uses the WebP files in your pages automatically, including in srcset.
Upload a JPEG and open it in the Media Library. The file URL should end in .webp.