この記事は最後に更新してから1年以上経過しています。

絵文字画像がPNGからSVGへ

説明

WordPressでは絵文字に対応していないブラウザ環境で絵文字を表示する仕組みをバージョン4.2から提供。絵文字に対応しているブラウザとそん色がない表示を実現する。

HTMLエンティティをimgタグに変換

その仕組みは、絵文字に該当する文字部分は😀のような文字エンティティに変換され、絵文字に対応していないブラウザでは文字エンティティを該当する画像ファイルを指定したimg要素に変換するというも。バージョン4.5.3では次のようなimg要素に変換されている。

<img draggable="false" class="emoji" alt="?" src="https://s.w.org/images/core/emoji/72x72/1f600.png">

※「?」はオリジナルの絵文字。

これに対しバージョン4.6(RC2)では画像ファイルがPNGではなくSVGに変わっている。

<img draggable="false" class="emoji" alt="?" src="https://s.w.org/images/core/emoji/2/svg/1f600.svg">

絵文字画像のURLをカスタマイズする

絵文字表示に関連するhead要素(一部抜粋)は次のようになっており、絵文字画像のURLが指定されていることがわかる(バージョン4.5.3の場合)。

<script type="text/javascript">
	window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/sekkyakucontest.jp\/wp\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.5.3"}};

これに対しバージョン4.6(RC2)では次に示すようにPNG用に加えSVG用のURLと拡張子の指定が追加されている。

<script type="text/javascript">
	window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/2\/svg\/","svgExt":".svg","source":{"concatemoji":"http:\/\/localhost\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.6-RC2"}};

これらのURLと拡張子はそれぞれフィルターにてカスタマイズできるようになっている。

$settings = array(
	'baseUrl' => apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/2/72x72/' ),
	'ext' => apply_filters( 'emoji_ext', '.png' ),
	'svgUrl' => apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ),
	'svgExt' => apply_filters( 'emoji_svg_ext', '.svg' ),
);

ブラウザがSVGをサポートしている場合は絵文字画像はSVGの設定が適用されると思われるので、PNG画像をカスタマイズしているサイトは適宜対応する必要があるだろう。

また絵文字表示に関連するhead要素部分を出力する関数名は、バージョン4.5.3では print_emoji_detection_script だったが、バージョン4.6(RC2)では _print_emoji_detection_script に変更され、アクセスはプライベートに変わっている。レアケースだと思うが、独自にprint_emoji_detection_script関数を呼び出している場合はバージョンアップによりエラーになるので注意してほしい。


最終更新 : 2018年05月27日 10:44

お勧め

get_block_editor_settings(2021年8月17日 更新)

array get_block_editor_settings( array $custom_settings, WP_Block_Editor_Context $block_editor_context )
ブロックエディター設定内容を取得する。

urldecode_deep(2019年3月13日 更新)

mixed urldecode_deep( mixed $value )
変数内の文字列についてURLデコード処理を行う。

user_can(2018年5月27日 更新)

bool user_can( mixed $user, string $capability )
ユーザの権限を調べる。

get_locale(2019年1月15日 更新)

string get_locale( )
現在のロケール情報('ja'や'en_US'など)を取得する。

get_comment_time(2023年4月3日 更新)

string get_comment_time( [ string $format = '' [ , bool $gmt = false [ , bool $translate = true [ , int | WP_Comment $comment_id = 0 ] ] ] ] )
コメント投稿日時を取得する。