Serve WebP images in WordPress without a plugin
WebP files are usually 25β35% smaller than JPEGs of the same quality, and all current browsers support them since Safari 14 joined in 2020. WordPress 5.8 (July 2021) added WebP to the allowed upload types, and with it a filter that makes the generated image sizes WebP even when the uploaded original is a JPEG. That covers almost every image shown on the site, without a plugin and without rewriting HTML.
Problem: The site serves JPEG and PNG images, PageSpeed Insights suggests “Serve images in next-gen formats”, and the host does not allow image optimisation plugins.
Solution: On WordPress 5.8+, map JPEG to WebP with the image_editor_output_format filter, so all resized copies of new uploads are created as WebP, and regenerate the sizes of existing images. Check first that the server’s image library supports WebP.
Check the server
<?php // In a temporary snippet or with wp eval var_dump( wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) );
true means Imagick or GD on the server can write WebP. If it is false, ask the host to enable WebP in GD or Imagick; without it nothing below works.
Generate the sizes as WebP
<?php
add_filter( 'image_editor_output_format', function ( $formats ) {
$formats['image/jpeg'] = 'image/webp';
return $formats;
} );
From now on, every size WordPress creates for an uploaded JPEG (thumbnail, medium, large and the theme’s own sizes) is a WebP file, and wp_get_attachment_image(), featured images and images in the content use them automatically, including srcset. The original upload stays a JPEG and is still used for the “Full size” option and for downloads.
PNG is left out on purpose: PNGs are often screenshots and graphics with transparency, where lossy WebP can look worse. Add $formats['image/png'] = 'image/webp'; if your PNGs are photos.
Existing images
The filter affects only images processed from now on. Regenerate the sizes of the media library:
wp media regenerate --yes
This creates new WebP sizes and updates the attachment metadata; posts that use the sizes through the block editor or wp_get_attachment_image() switch to WebP. Images inserted into old posts as hard-coded URLs of a JPEG size keep pointing to the JPEG files, which still exist.
Quality
WordPress uses quality 82 for generated sizes. For WebP it can be lower without visible loss:
<?php
add_filter( 'wp_editor_set_quality', function ( $quality, $mime_type ) {
return 'image/webp' === $mime_type ? 75 : $quality;
}, 10, 2 );
Compare a few typical images at 70, 75 and 82 before choosing; photos with fine textures need more than flat graphics.
Very old browsers
Internet Explorer and Safari before version 14 (macOS before Big Sur) cannot show WebP. For a site whose visitors still use them, keep JPEG sizes and serve WebP only to browsers that send image/webp in the Accept header, with server rules or a CDN that converts images on the fly.
NOTE: Converting is lossy: a WebP made from a JPEG is a second compression of an already compressed image. Upload originals in good quality and let WordPress create the WebP sizes from them, instead of uploading pre-compressed JPEGs and converting those.