Home > CMS Tutorial > WordPress > How to make WordPress support WebP format images

How to make WordPress support WebP format images

藏色散人
Release: 2020-11-05 16:16:41
forward
3051 people have browsed it

The following column WordPress Tutorial will introduce to you how to make WordPress support WebP format images. I hope it will be helpful to friends in need!

How to make WordPress support WebP format images

WordPress does not support WebP format image upload by default. Add the following code to the current theme function template functions.php to solve the upload problem.

function webp_filter_mime_types( $array ) {
$array['webp'] = 'image/webp';
return $array;
}
add_filter( 'mime_types', 'webp_filter_mime_types', 10, 1 );
Copy after login
function webp_upload_mimes($existing_mimes) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');
Copy after login

Although you can upload images in WebP format, you cannot see thumbnails in the media list. This is because WordPress uses the file_is_displayable_image() function to determine when using the wp_generate_attachment_metadata() function to generate image data. Whether the file is an image or not, the result of judging the WebP image is no, so the operation of saving the image data is interrupted.

This function is located at: wp-admin/includes/image.php Expand

The solution is to add the following code in the theme’s functions.php:

function webp_file_is_displayable_image($result, $path) {
$info = @getimagesize( $path );
if($info['mime'] == 'image/webp') {
$result = true;
}
return $result;
}
add_filter( 'file_is_displayable_image', 'webp_file_is_displayable_image', 10, 2 );
Copy after login
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);
Copy after login

in the text The illustration is a webp image. Although Qiniu, Youpaiyun, Alibaba Cloud oss, Tencent Cloud cos, etc. currently support WebP, we found that Apple devices do not support webp images, including the IOS version of WeChat. This may also be because WordPress has not supported it. The reason for webp pictures.

The above is the detailed content of How to make WordPress support WebP format images. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template