WordPress : apply_filter の動作を調査
※当サイトにはプロモーションが含まれています。
公開日:
更新日:
WordPress の apply_filter() の動作を調べたかったので、例として the_title()関数の動きを追ってみた。
調査対象
- WordPress 3.7.1
- テーマ:Twenty Thirteen
- 投稿ページにアクセスした時の content.php 上にある the_title() 関数の動作を調べた。
まとめ(アクセスした時の関連処理)
- default-filters.php 内でデフォルトフィルターがセットされる。(add_filter()でセット)
- “the_title” タグに対しては、4つの関数がセットされている。
- タイトルを表示する箇所で the_title()呼び出し → 内部で get_the_title()呼び出し → 内部で apply_filters()呼び出し
- 1でセットされた関数(4つ)が優先順位の順番で タイトル文字列に対して実行される。
フィルタフックに関数を登録しているところ
- wp-includes/default-filters.php
- WordPress では、wp-includes/default-filters.php でデフォルトのフィルタフック登録処理 (add_filter()) を行っている。
- このファイルは、wp-settings.php 上で require() されている。
- ここで、’the_title’ タグに対しては、以下の4つの関数が、add_filter()されている。
- capital_P_dangit
- wptexturize
- convert_chars
- trim
コールスタック
- wp-content/themes/twentythirteen/index.php
- wp-content/themes/twentythirteen/content.php : the_title()を実行
- wp-includes/post-template.php : function the_title() : get_the_title()を実行
- wp-includes/post-template.php : function get_the_title() : apply_filters()を実行
- wp-includes/post-template.php : function the_title() : get_the_title()を実行
- wp-content/themes/twentythirteen/content.php : the_title()を実行
the_title() 実行時 apply_filter() 内 $wp_filter[’the_title’] の値
- 配列になっている。
- 添字 10 (priority) には以下の関数名が登録されている。
- wptexturize
- convert_chars
- trim
- 添字 11 (priority) には以下の関数名が登録されている。
- capital_P_dangit
広告