簡體   English   中英

Wordpress替換圖像的默認寬度和高度值

[英]Wordpress replace default width and height values for images

我想替換Wordpress中可視編輯器添加的默認widthheight值。

我正在使用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;">

不用先交替,只需先匹配寬度然后再匹配高度並捕獲數字。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM