Skip to content

Instantly share code, notes, and snippets.

@mbparvezme
Last active July 4, 2021 08:09
Show Gist options
  • Save mbparvezme/beab467188ed63ff8b79f97018b57d59 to your computer and use it in GitHub Desktop.
Save mbparvezme/beab467188ed63ff8b79f97018b57d59 to your computer and use it in GitHub Desktop.
Upload WebP image files to Wordpress
// --- TESTED WITH WordPress 5.3.2
// --- ADD BELLOW CODE TO function.php
//** Enable upload for webp image files */
function webp_upload_mimes($existing_mimes) {
$existing_mimes['webp'] = 'image/webp';
return $existing_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);
add_filter('mime_types', 'webp_upload_mimes');
@artmaug
Copy link

artmaug commented Jul 3, 2021

Works perfect, thank you

@mbparvezme
Copy link
Author

Works perfect, thank you

Thank you. But this is not my writing. I collected it here after getting it online. But unable to remember the source.

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