画像挿入時に代替テキスト(alt属性)をtitle属性としても設定する
「メディアを追加」で画像を挿入する際「代替テキスト」は<img>タグのalt属性に設定されますが、これをtitle属性としても設定したい場合、functions.phpに下記のように記載します。
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * エディタでの画像挿入時、「代替テキスト」をtitle属性にも設定するようにします。 */ function image_send_alt_title( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ){ if ( $alt !== '' ) { return preg_replace( '#<img #', '<img title="' . $alt . '" ', $html, 1 ); } else { return $html; } } add_filter( 'image_send_to_editor', 'image_send_alt_title', 1, 8 ); |
上記のように代替テキストを入力して挿入すると、下記のようにタグが挿入されます。
1 |
<img title="代替テキストです。" src="http://****/***.png" alt="代替テキストです。" width="888" height="59" class="alignnone size-full wp-image-66" /> |