メディア追加時の自動で挿入される「画像挿入時にwidthとheightやCSSを削除する」方法

メディア追加時の自動で挿入される「画像挿入時にwidthとheightやCSSを削除する」方法をご紹介いたします。

wordpressのテーマ内にありますfunction.phpに以下を追記すると、メディア追加時にwidthとheightが入らずに更新できます。

 

add_filter( 'image_send_to_editor', 'remove_image_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_image_attribute', 10 );

function remove_image_attribute( $html ){
$html = preg_replace( '/(width|height)="\d*"\s/', '', $html );
$html = preg_replace( '/class=[\'"]([^\'"]+)[\'"]/i', '', $html );
$html = preg_replace( '/title=[\'"]([^\'"]+)[\'"]/i', '', $html );
$html = preg_replace( '/<a href=".+">/', '', $html );
$html = preg_replace( '/<\/a>/', '', $html );
return $html;
}

関連記事

  1. Advanced Custom Fieldsを活用した条件分岐 (中身…

  2. ZoomとgoogleMeet どちらが便利なのか比較!

  3. WordPressの常時SSL化手順について、とても便利なプラグインを…

  4. ホームページ制作は"お客様も一緒に"作らないと集客のできるサイトはでき…

  5. 別ページから特定のタブにダイレクトリンクする方法について[jQuery…

  6. インスタグラムのタイムラインをサイトに表示する方法