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

How to make WordPress support WebP format images

藏色散人
藏色散人 forward
2020-11-05 16:16:41 2944browse

The following columnWordPress Tutorialwill 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 );
function webp_upload_mimes($existing_mimes) { $existing_mimes['webp'] = 'image/webp'; return $existing_mimes; } add_filter('mime_types', 'webp_upload_mimes');

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

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!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete