LoginSignup
1

More than 5 years have passed since last update.

[WordPress]画像挿入時に、自動で付与されるclass、title、リンクを削除する方法

Posted at

WordPressでは、画像を挿入する時に、自動でalt、title、width、height、classなど属性が自動挿入されますが、alt以外はいらないってことの方が多くないですか?

通常画像を挿入すると、下のコードのような感じで、属性が挿入されます。

<a href="http://test.com/wp-content/uploads/2014/06/piano.jpg">
<img src="http://test.com/wp-content/uploads/2014/06/piano.jpg" 
alt="piano" title="piano" width="1000" height="667" class="alignnone 
size-full wp-image-x" /></a>

たまにしか更新しないんだったら、別にその都度削除すればいいんだけど、毎日更新したり大量に画像を使ったりするときは、めんどくさいっすね。
なので、消してしまいましょう。

alt以外削除する方法

以下のコードをfunction.phpに記述してください。
````
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( '//', '', $html );
$html = preg_replace( '/<\/a>/', '', $html );
return $html;
}
````

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1