wordpress去掉图片宽高及size属性以自适应手机的功能,把如下代码放到模板所在目录的functions.php里即可:
/**
* 去掉内容里图片的宽高属性
* @author albert
* @date 2021.07.08
*/
function ludou_remove_width_height_attribute($content){
preg_match_all("/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png\.bmp]))[\'|\"].*?[\/]?>/", $content, $images);
if(!empty($images)) {
foreach($images[0] as $index => $value){
$new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
return $content;
}
add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
/**
* 去掉内容里图片的size属性
* @author albert
* @date 2021.07.08
*/
add_filter( 'max_srcset_image_width', create_function( '', 'return 1;' ) );