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;">