Archived topic
Elements Page Hero serve WebP
3 replies · Started by Eric on October 6, 2020
Is there a way to serve a webp file that Imagify creates in the elements page hero to up my Google PSI score? What I am thinking is that in the css it could serve a webp background image first and then the jpg. Or if there is a way to serve it so imagify can rewrite the image to a picture with both options.
Hi,
By default, WordPress doesn't allow WebP files on to be uploaded on its media library. But you can still definitely do it by adding this PHP snippet.
//** *Enable upload for webp image files.*/
function webp_upload_mimes($existing_mimes) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');
//** * 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);
Reference: https://mariushosting.com/how-to-upload-webp-files-on-wordpress/
Alternatively, you can convert already uploaded jpg files to webp by using a third party plugin.
Read more here - https://kinsta.com/blog/webp/
Will this serve a webp with a jpg fallback?
Will this serve a webp with a jpg fallback?
I'm afraid the snippet is only for allowing you to directly upload your WebP files to the WordPress media library.
Custom image fallbacks to its jpg version will require third party plugins like the one you mentioned. (Imagify)
As to what's the best among the plugins, I'm afraid I can't pinpoint them for you as they're outside GeneratePress support's scope.