March 9, 2021

Textual description of firstImageUrl

How to convert and upload WebP images in WordPress

|
Last Updated: July 2024

Find out how to convert and upload WebP images in WordPress site.

If you still cannot upload images in WebP format to their WordPress, then follow this tutorial.

You can upload webp images in WordPress without the help of a plugin.

If you use .jpg and .png images in your WordPress posts, the page loading time is slower compared to JPEG 200 or WebP format.

When you check the blog post with Google PageSpeed Insights, you see a similar remark like this:

Serve images in next-gen formats 0.75 s

Image formats like JPEG 2000, JPEG XR, and WebP often provide better compression than PNG or JPEG, which means faster downloads and less data consumption

How to convert and upload webP images in WordPress

There are two steps in this tutorial.

First is to convert your jpg or png image file to webp format.

Then you have to add code to your WordPress functions.php file, so that it can accept webp images.

STEP 1: Convert PNG Or JPEG To WebP

To improve or reduce your loading page speed, you convert the image format from PNG or JPEG to WebP.

This is how you convert or change your .jpg and .png images to .webp files.

Go to this image online converter.

Click "Choose File".

Select the image file from your computer.

After it has finished uploading, click “Start conversion” button.

convert jpeg or png to webp

Once it has fully converted it to webP format, a box will pop up.

Then save the WebP image onto your computer or laptop.

Now it's time to upload your WebP image to your WordPress blog post.

When you try to upload image with WebP format in your WordPress site, you see this error message:

“Sorry, this file type is not permitted for security reasons”.

In other words, you cannot upload images in WebP format to your WordPress site.

STEP 2: Upload WebP image to WordPress

To overcome this technical problem, you need to add some code to your WordPress php file.

This is what you need to do:

Log in to your WordPress Dashboard.

Go to "Appearance", then click on "Theme Editor".

Look for "Theme Functions (functions.php).

Click on it.

The functions.php file with code will pop up.

There are two sets of code which you need to add or paste them at the end of the functions.php file.

The first set is the main code. It enables to upload webP image to wordPress.

It looks like this:

//** *Enable upload webp image to wordpress.*/

function webp_upload_mimes($existing_mimes) {

    $existing_mimes['webp'] = 'image/webp';

    return $existing_mimes;

}

add_filter('mime_types', 'webp_upload_mimes');

The second set of code is to enable to see your thumbnail preview on the Media/Library.

It looks like this:

//** * Enable preview / thumbnail for webp image in wordpress.*/

function webp_is_displayable($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', 'webp_is_displayable', 10, 2);

If you do not add this second set of code, you would NOT see any thumbnail image preview.

So, you need to add or paste the two sets of code onto your functions.php file.

Now add this two sets of code together at the end of the functions.php.

//** *Enable upload for webp image files.*/

function webp_upload_mimes($existing_mimes) {

    $existing_mimes['webp'] = 'image/webp';

    return $existing_mimes;}

add_filter('mime_types', 'webp_upload_mimes');

//** * Enable preview / thumbnail for webp image files.*/

function webp_is_displayable($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', 'webp_is_displayable', 10, 2);

Then click the "Update File" button.

That's all you need to do.

Now you can upload any WebP image to your WordPress site without any problem.

You can also view the preview image (thumbnail).

RELATED TIP: 

How to fix WordPress functions.php error: cannot log in and site disappear