首页 Wordpress wordpress 去除图片的样式及长宽属性

wordpress 去除图片的样式及长宽属性

默认插入图片的html结构
html
<img class="alignnone size-full wp-image-123" src="/xxx" alt="" width="XXX" height="XXX" />
而我需要的图片html结构为
html
<img src="/xxxx" alt="" />
需要在主题function.php里添加以下代码:
php
//去除图片高宽
add_filter( 'image_send_to_editor', 'fanly_remove_images_attribute', 10 );
function fanly_remove_images_attribute( $html ) {
	//$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
	$html = preg_replace( '/width="(\d*)"\s+height="(\d*)"\s+class=\"[^\"]*\"/', "", $html );
	$html = preg_replace( '/  /', "", $html );
	return $html;
}

相关推荐

发表回复

邮箱地址不会被公开。