Last update at :2024-05-10,Edit by888u
WordPress does not support WebP format image uploads 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 wp_generate_attachment_metadata()
function to generate image data. The >file_is_displayable_image() function determines whether the file is an image, and the result of determining the WebP image is no, thus interrupting the operation of saving the image data.
This function is located at: wp-admin/includes/image.php
- function file_is_displayable_image( $path ) {
- $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO );
- $info = @getimagesize( $path );
- if ( empty( $info ) ) {
- $result = false;
- } elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {
- $result = false;
- } else {
- $result = true;
- }
- /**
- * Filters whether the current image is displayable in the browser.
- *
- * @since 2.5.0
- *
- * @param bool $result Whether the image can be displayed. Default true.
- * @param string $path Path to the image.
- */
- return apply_filters( ‘file_is_displayable_image’, $result, $path );
- }
The solution is to add the following code to 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);
The illustrations in the text are webp images. Although Qiniu, Youpaiyun, Alibaba Cloud oss, Tencent Cloud cos, etc. currently support WebP, it is found that Apple devices do not support webp images, including the IOS version of WeChat. This is also Maybe it’s because WordPress has never supported webp images.
If you find it troublesome to change the code, you can install the plug-in: Allow Webp image
Recommended site searches: local IP query, Hong Kong cn2 server, domain name query network, site group server rental, Hong Kong's best virtual host, US free space, domain name registration official website, US domain name website, China IP segment, Ministry of Industry and Information Technology website registration,
发表评论