Normally, WordPress does not allow to uploading of .webp format image files. But in some cases, we need to use that format. In order to allow the format to be used in WordPress, we need to add some coding in functions.php in the active theme. The following code needs to be added in the bottom of all codes in functions.php in the active theme.
function webp_upload_mimes( $existing_mimes ) { // add webp to the list of mime types $existing_mimes['webp'] = 'image/webp'; // return the array back to the function with our added mime type return $existing_mimes; } add_filter( 'mime_types', 'webp_upload_mimes' );
These lines of code will allow uploading .webp format images but the thumbnail could not be seen from the WordPress dashboard. In order to view the thumbnail, we need to add an additional piece of code at just the bottom of the above code.
//** * 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);
Please note: These codes must have to be in functions.php in active theme.
Comments
Post a Comment