Wordpress webp image previews

I found a solution to show the thumbnails on the media manager. You have to add the following code to the functions.php of your active theme:

//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);

The webp_is_displayable function is using the file_is_displayable_image hook and checks if the file (on $path) is a webp image file. To check for the webp image file the function is using the constant IMAGETYPE_WEBP.

Tags:

Php

Wordpress