我已经使用以下代码更新了wordpress以允许webp上传,
function webp_upload_mimes( $existing_mimes ) {
$existing_mimes['webp'] = 'image/webp';
return $existing_mimes;
}
add_filter( 'mime_types', 'webp_upload_mimes' );这很好,但是webp图像没有在媒体选择器中显示预览,如图所示

我是否可以强迫wordpress呈现webp预览?当我的网站完成的时候,它可能会有数百张webp图片,在选择时不能看到它们可能会带来巨大的痛苦!
发布于 2019-01-30 18:24:05
更新2021年7月
从WordPress版本5.8向前看,您可以像今天的JPEG或PNG图像一样,在WordPress中上传和使用WebP图像(只要您的托管服务支持WebP)。切换到WebP格式为您的图像将改善您的网站的性能和网站访问者的经验。 https://make.wordpress.org/core/2021/06/07/wordpress-5-8-adds-webp-support/
我找到了一个解决方案来显示媒体经理的缩略图。必须将以下代码添加到活动主题的functions.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');
//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);webp_is_displayable函数使用file_is_displayable_image钩子并检查文件(在$path上)是否是webp图像文件。为了检查webp图像文件,函数使用常量IMAGETYPE_WEBP。
发布于 2020-07-15 16:47:51
Sebastian Brosch提出的解决方案仍然有效。但是正如S_R评论的那样,旧的webp图像不会有一个工作预览。为此,您可以使用wordpress插件“强制再生缩略图”重新加载旧图像并创建预览。
发布于 2020-07-14 12:16:36
还有一件事,如果您希望WordPress允许您将webp上传到媒体/库,您可以使用以下代码:
//** 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');https://stackoverflow.com/questions/54442929
复制相似问题