How to Serve WebP Format Images in WordPress

WebP is a modern format for serving images faster than ever. If you are using WordPress, you can easily serve all images in WebP with some basic tweaks.

Learn about Browser SupportBenefitsNGINXUse WebP in WordPress via CloudflareImplement WebP with FallbackConvert to WebP

Most Browsers support WebP

  • According to Caniuse data, WebP is currently supported in 91% browsers include Apple Safari, Google Chrome, Opera, Mozilla Firefox, Microsoft Edge and Android browsers.
  • You can still serve JPEG/PNG as fallback for unsupported browser.

Major Benefits of using WebP format Image

  • In comparison to the size of normal JPG or PNG image, same dimension image WebP can serve in small bytes. Hence, Images will load faster.
  • Serving Quality Images in few bytes, dramatically save bandwidth.
  • Keep your website updated with latest trend. Don’t loss conversation due to bull-cart slow loading issue.
  • WebP is recommended by Google Developers. Helps you in passing “serve images in next-gen format” recommendation of Google PageSpeed Insight.

This is how you can serve WebP for a WordPress site.

Use WebP Express Plugin in NGINX

  • Install & Activate WebP Express, free plugin. A huge thanks to Dev.
  • Operation mode: Varied image responses.
  • Scope: Upload only.
  • Run Bulk Convert
image 2
  • For Apache users, no config requires as .htaccess does all magics.
  • NGINX server user need to modify configuration file with root access.

For better organization of code, I would recommend place first inside /etc/nginx/ directory with name webp.conf then include in main server block.

Enter below command

cd /etc/nginx/ && nano webp.conf
  • Paste below code using right click inside nano editor in SSH Terminal.
# WebP Express rules
# --------------------
location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
  add_header Vary Accept;
  expires 365d;
  if ($http_accept !~* "webp"){

# Route requests for non-existing webps to the converter
location ~* ^/?wp-content/.*\.(png|jpe?g)\.webp$ {
# ------------------- (WebP Express rules ends here)
  • Press CTRL+O and Enter key to save.

Now visit main server block.

cd /etc/nginx/sites-available && ls

Highly recommend: Learn Installing WordPress at NGINX (In simple three steps)

Edit your configuration file, and put include webp.conf; as shown below.

# General
server {
    listen         80;
    server_tokens off;
    return 301 https://$host$request_uri;
server {
server_tokens off;
root /var/www/html;
index index.php index.html index.htm;
client_max_body_size 0;

    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_certificate /etc/comodo/cert.pem;
        ssl_certificate_key /etc/comodo/private.pem;
        ssl_prefer_server_ciphers on;
        ssl_session_cache   shared:SSL:20m;
        ssl_session_timeout 20m;
        ssl_ciphers 'TLS13+AESGCM+AES128:EECDH+AES128';

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

# WebP Express rule goes here

include webp.conf;

# WebP Rule end

location / {
    try_files $uri $uri/ /index.php$is_args$args;

Reload or restart the NGINX.

service nginx reload

Things to note

  • If you use BunnyCDN, must enable Vary Cache.
bunnycdn vary cache
  • Cloudflare doesn’t support Vary Cache. Try below alternative approach.

Use WebP using Cloudflare CDN

If you’re a Cloudflare Pro user you can simply enable WebP in one-click from Speed Tab.

cloudflare webp

Serve WebP using BunnyCDN Optimizer

BunnyCDN offers Optimizer services which comes with On-the-fly WebP serving solution. It’s one-click solution for $9.5/mo additional cost.

bunnycdn webp

Serve WebP using JetPack Plugin

  • Simply install and activate JetPack plugin
  • Enable the Site Accelerator.
jetpack webp

You may notice downgrade in image quality that can be fixed using below filter.

add_filter('jetpack_photon_pre_args', 'jetpackme_custom_photon_compression' );
function jetpackme_custom_photon_compression( $args ) {
    $args['quality'] = 100;
    $args['strip'] = 'all';
    return $args;

Serve WebP in NGINX using ShortPixel Plugin

ShortPixel plugin can help in bulk image optimization with WebP conversion and serving as per Browser support. The best part this plugin does processing on their server so it won’t slow down your site.

shortpixel webp
  • If you’re Apache web server user you can use .htaccess rewriting. That’s simple.
  • In case of NGINX you can use below rewriting rule with the help of hosting support
  • This plugin is supported with WP Rocket cache plugin as well.

First, add this block before the server directive:

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

Add this block inside the server directive:

location ~* ^(/wp-content/.+)\.(png|jpe?g)$ {
    set $base $1;
    set $webp_uri $base$webp_suffix;
    set $webp_old_uri $base.$2$webp_suffix;
    set $root "<<FULL PATH OF wp-content PARENT>>";
    root $root;
    add_header Vary Accept;
    if ( !-f $root$webp_uri ) {
        add_header X_WebP_SP_Miss $root$webp_uri;
    try_files $webp_uri $webp_old_uri $uri =404;

Placement matters. So add it carefully.

Manual method

This section is for information purpose only.

Step 1 : Adding WebP format in HTML Document

First, you need to convert your all images in WebP and along with your previous image format as the fall-back. There is  some plugin like Optimus which can do this job automatically. But, I will show an another easy to do this manually.

  1. Go to this website
  2. Paste your Image Link and click on convert. Your WebP format images will be downloaded
  3. Now edit the raw HTML code where your normal Image is appearing.

Let say, in beginning your Image HTML code was like this

<img src="" alt="gulshan kumar" width="186" height="66" />

You need to wrap above code with little more HTML markup.

	<source srcset="" type="image/webp" />
	<img src="" alt="gulshan kumar" width="186" height="66" />

Now, Your HTML document is ready to serve images in WebP format.
Step 2 : Configure server settings
Just one more step, you need to configure some Apache Webserver settings via .htacccess so browser and web server can treat it properly like all other images.

Your Web Hosting server may don’t know from which mime type this kind of format images they need to serve. So must add proper mime type. Also, it would be worth to setup expiry header for caching.

# Serve Images with correct Mime Type
AddType image/webp .webp

# Setup Cache
ExpiresActive On
ExpiresByType image/webp A2592000

Kindly note: WordPress by default do not support uploading of WebP format image. You may get error “This file type is unfortunately not allowed for security reasons” while uploading .webp images.

So, must fix this issue by adding this code in your theme functions.php It would be helpful in case if you will upload your images directly from WordPress Dashboard > Media option.

function webp_upload_mimes( $existing_mimes ) {
	// add webp to the list of mime types
	$existing_mimes['webp'] = 'image/webp';

	// return the array back to the function with our added mime type
	return $existing_mimes;
add_filter( 'mime_types', 'webp_upload_mimes' );


If you need any help, please look at comment section or join my forum. It would be my pleasure to help you.

Further readings
If you are curious to learn more about WebP, please refer these links.


Any question? Please ask at Gulshan Forum.

Leave a Comment

31 thoughts on “How to Serve WebP Format Images in WordPress”

  1. The problem with the suggested HTML implementation is that Chrome will make two requests, one for WEBP and another one for JPEG resulting in double requests and increasing the page load time.

    • Hey dear,

      It doesn’t create two requests(as you are saying) instead it automatically serve only one type image format either WebP or JPEG according to HTTP accept type.

      I’d suggest please try one more time and look carefully at Network Timeline in the browser console. If you see there are double requests, please email me your HTML code and timeline screenshot also. I’d love to review it. I believe it should work fine for you too.

      Thanks & Regards,

      • I installed the code into the functions.php. And as you said it did allow me to upload in my WP theme. The problem is that the image doesn’t allow cropping so it won’t allow me to use it on the site.

  2. I haven’t added anything except the last code from your post in function.php to support Webp format on all of future media uploads. Do I need to add any thing else ? or will it create any kind of issue for users whose browsers are yet unsupported to this format?
    anyway keep up the good work 😉

    • That’s all. Now you can easily upload webp directly in your WordPress Dashboard. No extra things to do. Just feel free to use it with picture HTML tag. So, it will support for all browsers, without any issues. Thanks

  3. Hi Gulshan,

    Even after putting the code snippet in the function.php file.

    I still have the same error message when I try to upload webp format. The message is “sorry this file type is not permitted for security reasons”.

    Can you help fix this?



    • Hello Martin,

      At my end it’s working fine. For a demo, I have sent a quick video on your email. Please check and revert back if I can help with any thing else.


  4. Hi..Just wondering if i need to edit each image tag or is there somewhere in my theme templates this could be done.
    Any guidence would be great.

  5. Hello Gulshan,

    Thanks for sharing this article.

    I did follow the steps mentioned in here in this format.

    But the image is not appearing on Firfox, and IE. Did I miss any thing in here.

    Please suggest.

  6. Hi,

    I am not quite clear on where this needs to be inserted. I am looking to Optimise my header image to Webp. Is there a video or screenshot based write up on how I can include this?

    Would really appreciate the help


  7. It works great in Chrome and Firefox on the PC/Laptop. BUT, the webp images are not displayed on iPhone (Chrome and Safari)

    • Safari is not supporting webP. Chrome on iOS is based on Safari, too. So webP won‘t work on iOS at all and you have to serve a fallback image.

  8. I have tried this method to enable WebP format images on my site, but I am encountering some unknown error. My site showing a low score on google page speed insight.

    • May I know please which specific method you have tried? Also, please share the screenshot of error. I will try my best to help you.

  9. Hey Gulshan Kumar ,

    Awesome post with good information. It is my first time commenting on your blog post and am glad to say that you have done a fabulous work and provided helpful & easy tips to serve webp format images in WordPress. I really like and appreciate the way you have explained the things in such a step-by-step process making the concept very clear and easy to understand. while scrolling through your blog post i have noticed that you have also discussed about the major benefits of using webp format image that are also providing good understanding. You have explained all the vital things in an easy language that are true enough to understand and educate the readers.

    After going through this complete guide i really gain ideas and am sure that this post is definitely going to help lots of people & especially WordPress-users to sort out their query. Good work and keep sharing more similar post.

    Eventually thanks for sharing your knowledge, ideas and such an informative post.


  10. Hi, this is a good article, but
    It is better to install the plugin or put the code directly for webp
    This way, each image does not create two urls. Does it cause a problem in the SEO site?