Include WebP images in WordPress

“WebP is an image format employing both lossy and lossless compression. It is currently developed by Google, based on technology acquired with the purchase of On2 Technologies.” – Wikipedia

As the WebP is not widely supported, you need to use polyfills or workarounds to actually use it. But it makes a lot of sense when you are building image heavy applications for the web. It decreases file-sizes immensely and supports transparency as PNG does.

To allow WebP upload in WordPress, add this to your functions.php:

  1. add_filter('upload_mimes','webp_mime_type');
  2.  
  3.     function webp_mime_type($mimes){
  4.  
  5.         return array_merge($mimes,array (
  6.  
  7.             'webp' => 'image/webp'
  8.  
  9.         ));
  10.  
  11.     }

Browsers with native support will show WebP images natively, for browsers without support deploy WebPJS developed by Google. Download and add it to your theme functions.php

  1. function webp_script() {
  2.  
  3.     // Loads JavaScript file for webp image support in all browsers 
  4.  
  5.     wp_enqueue_script( 'webpjs-script', get_template_directory_uri() . '/js/webpjs-0.0.2.min.js');    
  6.  
  7. }
  8.  
  9. add_action( 'wp_enqueue_scripts', 'webp_script' );

This should do the trick and enable WebP for all modern browsers, see browser support on the WebPJS development page.

Here another little nice trick to check for WebP using Javascript and replace webp with png images.

  1. window.testWebP = function(callback) {
  2.     var webP = new Image();
  3.     webP.onload = webP.onerror = function () {
  4.         callback(webP.height == 2);
  5.     };
  6.     webP.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IC4AAACyAgCdASoCAAIALmk0mk0iIiIiIgBoSygABc6WWgAA/veff/0PP8bA//LwYAAA';
  7. };
  8.  
  9. window.testWebP(function(support) {
  10.  
  11.    if(!support){
  12.                  $('img[src$=".webp"]').each(function(index,element) {
  13.           element.src = element.src.replace('.webp','.png');
  14.         });  
  15.         }
  16. });

Enjoy
Alex

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

5 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

3 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

5 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago