[英]Wordpress replace default width and height values for images
I want to replace the default width
and height
values that are added by the visual editor in Wordpress. 我想替换Wordpress中可视编辑器添加的默认
width
和height
值。
I am using the add_filter
function: 我正在使用
add_filter
函数:
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
and my function is: 我的功能是:
function remove_width_attribute( $html ) {
$html = preg_replace( '/\s*(?:(?:width=|height=)"(\d*)")/', 'style="max-width: ${1}px; max-height: ${2}px;"', $html );
return $html;
}
Input string is: 输入字符串为:
<img src="path/to/image.png" alt="" width="300" height="71" class="random-class pull-left" />
May also contain a
tags wrapped around. 也可能包含包裹
a
标签。
I want the output to be: 我希望输出为:
<img ..... style="max-width: 300px; max-height: 71px;">
不用先交替,只需先匹配宽度然后再匹配高度并捕获数字。
$html = preg_replace('/width="(\d+)"\s*height="(\d+)"/', 'style="max-width: $1px; max-height: $2px;"', $html);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.