Media PHP Intermediate

Convert JPEG and PNG Uploads to WebP

Make WordPress save the resized versions of uploaded JPEG and PNG images as WebP, which is usually 25–35% smaller.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 5 lines
add_filter( 'image_editor_output_format', function ( array $formats ): array {
	$formats['image/jpeg'] = 'image/webp';
	$formats['image/png']  = 'image/webp';
	return $formats;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • Your server’s image library must support WebP. Check Tools → Site Health → Info → Media Handling.
  • The untouched original upload is kept in its original format.
  • PNGs with transparency stay transparent in WebP.
  • Only new uploads are converted. Use a regenerate thumbnails plugin for older images.

How to test it

Upload a JPEG and open it in the Media Library. The file URL should end in .webp.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only