- Log in to your WordPress admin area
- Appearance
- Theme Editor
- Theme Functions [ functions.php]
- Copy and Paste this below-given Code and click on update file
- You are done, now you will be upload WebP files to your WordPress website
Add this below given code to your theme functions (functions.php)
function webp_upload_mimes( $existing_mimes ) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
}
add_filter( 'mime_types', 'webp_upload_mimes' );After adding this code you will be upload WebP images to your WordPress but you cant preview these images on your media library. If you want to see an image preview of these files on your media library you have to add some extra code to your Theme Functions (functions.php) of your WordPress Theme, The code is given below.
Add the below given code to your theme functions (functions.php) to preview webp images on your media library 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);https://www.roseblogging.com/how-to-...-to-wordpress/