0
我想替换由Wordpress中的可视编辑器添加的默认值width
和height
值。Wordpress替换图像的默认宽度和高度值
我用add_filter
功能:
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('/\s*(?:(?:width=|height=)"(\d*)")/', 'style="max-width: ${1}px; max-height: ${2}px;"', $html);
return $html;
}
输入的字符串:
<img src="path/to/image.png" alt="" width="300" height="71" class="random-class pull-left" />
还可能含有a
标签缠。
我所要的输出是:
<img ..... style="max-width: 300px; max-height: 71px;">