EWWW IO and WebP Images

Unlike the other conversion options in the EWWW IO plugin for WordPress, WebP conversion is possible for every PNG, JPG, and GIF image on your entire WordPress site. It is not limited to the Media Library. So theoretically, you could put the root path to your WordPress site in folders to optimize, run a Bulk Optimize, and EWWW IO would create WebP copies of all images where WebP is the smaller of the two formats.

Easy IO Sites

When Easy IO is enabled on your site, no further configuration is necessary. You do not need any other WebP options, and you do not need to run a Bulk Optimize. All WebP images will be stored on our CDN servers, which saves you space, and a lot of time. You can stop reading now, and go do something fun!

WebP Conversion

Converting JPGs to WebP involves a slight quality loss because we use the lossy WebP encoding for JPG images. On the other hand, converting a PNG to WebP is completely lossless, but if you want more compression, you can use the EWWW_IMAGE_OPTIMIZER_LOSSY_PNG2WEBP override (define it as true in your wp-config.php). WebP conversion does NOT replace your images, it only makes a copy of the original with the exact same name, but in the WebP format and with a .webp extension added onto the end.

It is important to note that all modern browsers currently support WebP. However, there will likely be folks visiting your pages with older versions of those browsers, so determining how to deliver WebP images is somewhat up to you.  By default, the plugin will only create the WebP images if they are smaller than the original format (JPG or PNG), so any delivery method should check to see if a webp image exists, which isn't possible if the only copy of an image is on a remote server. There are several rewriting options available in the plugin, Apache/Litespeed rewrite rules (.htaccess), JS WebP Rewriting, and <picture> WebP Rewriting.

SiteGround Hosting

If you use SiteGround (and their new Site Tools console), your site should have built-in WebP delivery, with no further action required (beyond converting your images to the WebP format).

Rewriting with Apache & Litespeed

The first solution is for folks using Apache (or Litespeed) with mod_headers and mod_rewrite. The EWWW IO plugin can attempt to modify the rewrite rules in your .htaccess file to send WebP images to supported browsers (if such a file exists). When you enable WebP conversion, the rewrite rules will be displayed, but only if your site does not use Cloudflare. The plugin will also display a test image by the WebP settings: a red PNG image indicates the server is not sending WebP images, or your browser does not support them.

When you insert the rules, you will get a success message if it worked, and it will attempt to reload the test image. It may sometimes be necessary to reload the page to get the right test image to load. When you're using a native Apache or Nginx rewrite solution like this, the image URLs in your page will not change. The rewriting happens (in place) when the visitor's browser actually requests the image.

EWWW IO will also attempt to automatically verify whether your server is delivering WebP images properly.

This video demonstrates two of the WebP delivery methods, and how to verify that they are working (<picture> WebP can be verified much like JS WebP, by looking at the actual filenames):

If you're running a multisite WordPress install, you may have to put the rewrite rules above your Wordpress rules for them to take effect. And if the rewrite rules aren't working, you can try the JS WebP Rewriting instead. 

Rewriting (via map) with Nginx

EWWW IO can't directly modify your Nginx configuration, so the solution is a bit different for Nginx. You need to potentially modify three different files here:

First, we need to add a map directive to your http config, usually /etc/nginx/nginx.conf (make sure you put it inside of the http{} section/block):

map $http_accept $webp_suffix {
  default "";
  "~*webp" ".webp";
}

That tells Nginx to set $webp_suffix to ".webp" if the browser's Accept header contains "webp". The second change is to be sure that Nginx knows what a .webp file is. While newer versions of Nginx should already recognize the WebP format, we need to modify the mime.types file (also in /etc/nginx/) to tell Nginx about this new file type. Look for this line, and add it if you can't find it:

image/webp	webp;

The last change is to setup a location block within your server block to handle PNG and JPG images that might have WebP versions. If you only have one server block, it is usually located in /etc/nginx/sites-enabled/default. Add this within the server {} section:

location ~* ^.+\.(png|jpe?g)$ {
  add_header Vary Accept;
  try_files $uri$webp_suffix $uri =404;
}

This tells Nginx to apply the enclosed rules to any files that end with png, jpg, or jpeg. If you have .jpe files, add a second question mark after the g, so it looks like "jpe?g?". The first rule adds the "Vary Accept" header to the return response, which is what allows supported CDNs to cache different files for the same URL. The second rule tells Nginx to look for a file that matches the original URL/URI with our $webp_suffix appended (.webp), and if that fails, to just deliver the original URI or fallback to a 404 if the file doesn't exist at all.

JS WebP Rewriting

If your web host features any sort of server-level caching solution, you deliver your images through a CDN (content distribution network), or you use a cloud security service like Cloudflare or Sucuri, you may not be able to use the rewrite rules. Instructions for known-working CDNs can be found below.

For such cases, JS WebP can be used to dynamically change the image URLs based on whether the visitor's browser supports the WebP format. This method may not work with certain themes/plugins, especially if another plugin merges the EWWW IO JavaScript in with other scripts.

You may skip any individual images from JS WebP Rewriting by use of the 'ewww_image_optimizer_skip_webp_rewrite' filter. The second parameter to the filter is the image URL, and returning true will skip that image.

Lazy Loading

When JS WebP is used with our built-in lazy loader, the resulting HTML is compatible with a wider range of themes. But if you insist on using a third party lazy-load plugin, it must match the patterns used by these 3rd-party plugins.

Server-level Caching

WP Engine uses server-level caching on all accounts by default. You can ask them to setup WebP rewriting rules for you, or just use JS WebP Rewriting.

Known working CDNs

The Cloudfront CDN from Amazon (not to be confused with Cloudflare) can support WebP images without the JS WebP (unless it is used with S3, see next paragraph for that scenario). To enable Cloudfront to operate with any of the rewriting rules above, you need to make one small change in your Distribution settings. Under the Behavior tab, select the Behavior and click the Edit button. Choose Whitelist from Forward Headers, and then add the "Accept" header to the whitelist.

If you use Cloudfront with Amazon S3, it can get a bit more complicated, unless you use WP Offload Media. If you are using WP Offload Media, and have local copies of your images, you can just enable JS WebP Rewriting and EWWW IO will auto-configure itself to work with your S3/Cloudfront URLs. If you use WP Offload Media, and do NOT have local copies of images on your web server, you need to enable Force WebP, run a bulk optimize, and then enable JS WebP Rewriting.

If you use a different Cloudfront/S3 plugin or setup, you will need to use the WebP URLs setting detailed below. If you do not keep local copies of your images on your web server, you will also need the Force WebP option. Additionally, if you use Force WebP mode, you must run a bulk optimize to make sure every single image has a .webp copy, because the JS WebP Rewriting will blindly rewrite every Cloudfront image url to point at the .webp version (for supported browsers only though). Once you know that all your images have .webp copies, you can then enable JS WebP Rewriting and configure WebP URLs as described below. 

In any case, the easiest method with Amazon S3 is to use Easy IO in front of your S3 bucket instead of Cloudfront, with no extra configuration.

StackPath & MaxCDN have an option within the zone/site settings to support WebP images as well. They don't create any .webp images, so you'll need to generate those on the origin/local server using the bulk optimizer, and then implement the .htaccess rules for Apache, or the above rules for Nginx.

BunnyCDN has an option within the Pull Zone configuration called Dynamic WebP Caching which allows you to use server-side rewrite rules in Apache/Litespeed & Nginx.

KeyCDN has a Cache Key WebP setting to support WebP images. Similar to StackPath, KeyCDN does not create any .webp images, so you'll need to generate those on the origin/local server using the bulk optimizer, and then implement the .htaccess rules for Apache, or the above rules for Nginx.

Cloudflare requires the JS WebP Rewriting function or you can use their Polish feature, which is available on their paid plans.

The Cloudways CDN has an option to support WebP images without JS WebP. For Cloudways hosting, you need to use the .htaccess rules, and then ask them to exclude images from the Nginx vhost. Then, you also need to enable the option for WebP support on their CDN.

If your CDN does not honor the Accept header, you should use the JS WebP Rewriting option. The same applies if you have any sort of proxy/caching server that is in front of your actual web server. The alternative method is a JavaScript solution that will parse your page looking for images that have WebP copies. There is also a WebP option in our SWIS Performance plugin that works nicely with page caching and proxy servers, but it still requires local copies of the images. Which leads us to the other two options...

WebP URLs & Force WebP

So, what are these other two options for? Let's address WebP URLs first, and then come back around to Force WebP, which is our last resort. Note that these options are only available in Ludicrous Mode, though they will be auto-configured if you use WP Offload Media.

The WebP URLs setting is needed if your image URLs (addresses) are rewritten to point at your CDN before JS WebP Rewriting runs. The JS WebP parser needs to reverse engineer your URLs back to file-system paths, so that it can check if a .webp image exists. For example, it would try to convert https://example.com/wp-content/uploads/2024/10/image.jpg to something like /var/www/wp-content/uploads/2024/10/image.jpg so that it can check to see if /var/www/wp-content/uploads/2024/10/image.jpg.webp exists. It does so by replacing your normal site URL (https://example.com) with the location of your website on your server (/var/www/ in the example). BUT, if your images are on a CDN, then the plugin needs to know what domain name to look for instead of example.com.

So, let's say your CDN domain is cdn.example.com, then you would simply enter https://cdn.example.com/ in the WebP URLs setting.

However, if you use a different folder on your CDN than you do on your server, then you need to include the folder too. If your CDN image URLs look like https://cdn.example.com/ files/2024/10/image.jpg compared to https://example.com/wp-content/uploads/2024/10/image.jpg, then you want to add https://cdn.example.com/files/ in the WebP URLs.

As mentioned, none of this will work if you do not have local copies of your images. To get around that requirement, you first need to use the Force WebP option to make EWWW IO create WebP versions of every image on your site. Then, you need to add your CDN URL in the WebP URLs box. Do not add your CDN URL to the WebP URLs until after you have used the bulk optimizer to create copies of all your images in the WebP format, or you'll have broken images on your site.

Once these options are setup properly, you can enable JS WebP Rewriting, and then the plugin will look for any images that match the URL patterns you’ve listed. It still provides a fall-back for browsers like Internet Explorer, so you can go do cartwheels (or whatever you enjoy)!

AMP (Accelerated Mobile Pages)

AMP has this fabulous 'fallback' attribute that allows you to wrap one image element inside another. Then if the first image cannot be displayed, the second one loads instead. The AMP Project Docs even show an  example of this, but it doesn't really work properly. The problem is that unsupported browsers will still load the webp image, and only then, after you've wasted precious mobile data, does it load the original (fallback) image. I wish it were not so, but until all AMP browsers support WebP, we'll just have to leave them untouched.