2014-09-12 49 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);