让你的WordPress支持webp图片格式上传到媒体库

webp图片格式体积更小,图片清晰度质量也不会相差多少,很适合做图片站使用,目前大部分主流浏览器也都已经支持webp格式了,但是Wordpress程序还不支持这个格式,所以也要自己修改,修改程序和主题的functions文件就行。

一、修改程序的functions.php文件

wp-includes/functions.php
搜索:

'image/jpeg' => 'jpg',

添加:’image/webp’ => ‘webp’,

搜索:

'jpg|jpeg|jpe'                 => 'image/jpeg',

添加:’webp’ => ‘webp’,

二、修改主题functions.php文件

新增:

//支持WebP格式上传
function bzg_filter_mime_types( $array ) {
	$array['webp'] = 'image/webp';
	return $array;
}
add_filter( 'mime_types', 'bzg_filter_mime_types', 10, 1 );
//支持WebP格式媒体库显示
function bzg_file_is_displayable_image($result, $path) {
	$info = @getimagesize( $path );
	if($info['mime'] == 'image/webp') {
		$result = true;
	}
	return $result;
}
add_filter( 'file_is_displayable_image', 'bzg_file_is_displayable_image', 10, 2 );
至此,wordpress已经支持webp图片格式