移除WordPress上传图片的宽度和高度参数

我们都知道在wordpress后台上传的图片添加到日志内会附带宽高属性。虽然在 HTML 代码中直接为图片 img 标签书写宽高属性会为提高页面加载速度,但有时我们出于种种目的仍是需要去掉 WordPress 后台上传的的图片的宽高参数。那么我们该怎么做呢?

仍是需要在万能的 functions.php 上做文章,在其中加入下面的代码即可。

// Remove IMG Width Height
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

这样我们就无需手动删除图片上的宽高属性,直接动态的将图片的宽度高度参数抹去了!

 

给力代码来自:http://www.wondercss.com/2013/wordpress/1544/

感谢原作者分享.

小 虾

哦也,我是小虾

You may also like...