Skip to content

Instantly share code, notes, and snippets.

@loorlab
Last active February 7, 2024 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loorlab/b7da9cf37a911138ba704f3080de659c to your computer and use it in GitHub Desktop.
Save loorlab/b7da9cf37a911138ba704f3080de659c to your computer and use it in GitHub Desktop.
Allow WebP Upload in WordPress
<?php
//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');
// functions.php
//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);
?>
@loorlab
Copy link
Author

loorlab commented Feb 7, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment