In this article, we will discuss an easy way to upload WEBP files in WordPress so that your website remains fast even when using many images.
Why use the WEBP format?
When using images in the WEBP format, you can use images with the same quality as PNG, JPG, and JPEG formats, but with a smaller file size.
With the WEBP format, you can reduce file size by up to 1.42x compared to JPEG, 1.70x compared to PNG, and 1.75x compared to AVIF. Here is an example of an image in PNG format and after it has been compressed into WEBP.
In this image, I used a JPG image with a size of 1.34MB, dimensions of 3840x2400, and a resolution of 96 DPI.
As you can see, there is no difference between the two images above. However, in terms of size, the two images have a very significant difference, where the JPG image is 1.34 MB while the WEBP image is 777KB.
How to Upload WEBP in WordPress
Sejak Versi 5.8, WordPress sudah default mengizinkan upload file .webp. Apabila kamu menggunakan versi ini atau lebih tinggi, maka kamu tidak perlu menambahkan kode tertentu untuk bisa upload file webp.
By default, WordPress prevents your website from uploading images in the WEBP format. To upload WEBP files, you need to use a plugin or add extra lines of code so that you can upload images in the WEBP format.
Prerequisites for Uploading WEBP Images
- Images that have already been converted to the WEBP format
- Website using the WordPress CMS
- Access to the website as an admin
- Not using the Twenty-Twenty Two, Twenty-Twenty Three, or newer themes (because it is more difficult to modify theme files, and it has not been tested)
- Using a Child Theme (Optional)
Add Filter Code
The first thing you need to do is add code so that WordPress can accept WEBP files and display a preview of those images.
- Log in to your WordPress dashboard, which is typically at yourdomain.com/wp-login.php or yourdomain.com/wp-admin/
- Open the menuAppearance > Theme File Editor
- Find and edit the filefunctions.php
- Add the following lines of code to the last line of the filefunctions.php
/* Mengizinkan Upload File WEBP */function wg_upload_webp( $existing_mimes ) { // Menambahkan webp ke daftar mime $existing_mimes['webp'] = 'image/webp'; // Mengirimnkan array kembali ke fungsi yang sudah ditambahkan mime webp return $existing_mimes;}add_filter( 'mime_types', 'wg_upload_webp' );
//** * Menambahkan preview untuk gambar dengan format WEBP.*/function wg_show_webp($result, $path) { if ($result === false) { $displayable_image_types = array( IMAGETYPE_WEBP ); $info = @getimagesize( $path ); if (empty($info)) { $result = false; } elseif (!in_array($info[2], $displayable_image_types)) { $result = false; } else { $result = true; } } return $result;}add_filter('file_is_displayable_image', 'wg_show_webp', 10, 2);How to Convert Images to WEBP?
To convert images to WEBP, you can use various tools that are currently available online. Here is a list of services that can help you convert images to WEBP before uploading them to WordPress.
COMMENTS
Loading...