WordPress支持webp格式图片的代码

相信很多站长都发现了,有时候WordPress并不支持webp格式图片的上传,这就比较尴尬了,毕竟webp格式还是使用广泛的,那么有解决方法吗?其实是有的,只要代码就能实现。

将如下代码加入主题的functions.php:

//让WORDPRESS支持WEBP格式图片
function webp_images_mime_types( $result ) {
    $result['webp'] = 'image/webp';
    return $result;
}

function webp_images_displayable($result, $path) {
    $info = @getimagesize( $path );
    if($info['mime'] == 'image/webp') {
        $result = true;
    }
    return $result;
}

add_filter( 'mime_types', 'webp_images_mime_types', 10, 1 );
add_filter( 'file_is_displayable_image', 'webp_images_displayable', 10, 2 );

版权声明:本文由 鹿泽笔记 原创发布。

本文链接:https://www.bailuze.com/17850.html

许可协议:本文标题《WordPress支持webp格式图片的代码》,转载请注明文章出处及原始链接。

鹿泽
鹿泽

深耕 SEO优化 十二年,从个人博客爱好者到专业谷歌优化顾问的成长之路。技术派SEO、全域流量技术运营专家,拥有多年从业经验,热衷于分析算法逻辑与推理的精准匹配。