2014-09-12 71 views
0

我想替换由Wordpress中的可视编辑器添加的默认值widthheight值。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;"> 

回答

1

除了使用交替的,只是比赛的宽度,则高度并捕获数字。

$html = preg_replace('/width="(\d+)"\s*height="(\d+)"/', 'style="max-width: $1px; max-height: $2px;"', $html);