WordPressのカスタマイズにおいて非常に便利なアクションフック。
どのようなアクションフックが存在しているのかは、Codex日本語版:プラグイン API/アクションフック一覧 のページを見ていただければ分かりますが、めちゃくちゃ多いです。
これらアクションフックを使いこなせるかどうかで、制作できるホームページの幅がかなり影響されますよね。
そこで、今回はアクションフックについて少しでも知識をつけていこう! ということで、アクションフックの実行順序について検証していこうと思います。
全部だと種類が多すぎてややこしいので、今回は、フロント表示側で実行されるアクションのうち18個のアクションフックについて検証してみました。
目次
主要なアクションフックの実行順序(検証結果)
先に今回の検証で確認できた実行順序をまとめておきますね。
- after_setup_theme
- set_current_user
- registered_post_type(複数回実行されます)
- registered_taxonomy(複数回実行されます)
- init
- wp_loaded
- parse_request
- send_headers
- parse_query
- pre_get_posts
- the_posts
- wp
- admin_bar_init
- template_redirect
- wp_enqueue_scripts
- wp_print_scripts
- wp_head
- wp_footer
- shutdown
どのように検証したかが気になる人は続きも読んでいただければと思います。
検証で実行したコード
実行順を調べたい各アクションフックにて文字列を追加していき、最後のアクションフックでそれらの文字列を出力させるクラスを用意しました。
'shutdown'
というアクションフックが一番最後に実行されるということはすでに知っていたので、そのタイミングで文字列を出力させています。
実行順検証用コード
class Echo_Fook_Order {
public $result = '';
public function __construct() {
$this->do_test();
}
private function do_test() {
add_action( 'after_setup_theme', function() {
$this->$result .= '<br><hr> after_setup_theme <hr><br>';
});
add_action( 'init', function() {
$this->$result .= '<br><hr> init <hr><br>';
});
add_action( 'parse_request', function() {
$this->$result .= '<br><hr> parse_request <hr><br>';
});
add_action( 'parse_query', function() {
$this->$result .= '<br><hr> parse_query <hr><br>';
});
add_action( 'send_headers', function() {
$this->$result .= '<br><hr> send_headers <hr><br>';;
});
add_action( 'pre_get_posts', function() {
$this->$result .= '<br><hr> pre_get_posts <hr><br>';
});
add_action( 'the_posts', function() {
$this->$result .= '<br><hr> the_posts <hr><br>';
});
add_action( 'admin_bar_init', function() {
$this->$result .= '<br><hr> admin_bar_init <hr><br>';
});
add_action( 'wp_loaded', function() {
$this->$result .= '<br><hr> wp_loaded <hr><br>';
});
add_action( 'wp', function() {
$this->$result .= '<br><hr> wp <hr><br>';
});
add_action( 'wp_enqueue_scripts', function() {
$this->$result .= '<br><hr> wp_enqueue_scripts <hr><br>';
});
add_action( 'wp_print_scripts', function() {
$this->$result .= '<br><hr> wp_print_scripts <hr><br>';
});
add_action( 'template_redirect', function() {
$this->$result .= '<br><hr> template_redirect <hr><br>';
});
add_action( 'wp_head', function() {
$this->$result .= '<br><hr> wp_head <hr><br>';
});
add_action( 'wp_footer', function() {
$this->$result .= '<br><hr> wp_footer <hr><br>';
});
add_action( 'registered_post_type', function() {
$this->$result .= 'registered_post_type<br>'; //登録された投稿タイプの数だけ実行される
});
add_action( 'registered_taxonomy', function() {
$this->$result .= 'registered_taxonomy<br>'; //登録されたタクソノミーの数だけ実行される
});
add_action( 'set_current_user', function() {
$this->$result .= '<br><hr> set_current_user <hr><br>';
});
add_action( 'shutdown', function() {
$this->$result .= '<br><hr> shutdown <hr><br>';
echo $this->$result; //最後に出力させる
});
}
}
new Echo_Fook_Order;
以上のように、19種類のアクションフックを実行させています。
出力結果が見やすいように <br>
タグと<hr>
タグを付与させて文字列を追加させていますが、registered_post_type
とregistered_taxonomy
だけは複数回実行されてしまうようなので、<hr>
タグは削除しています。
コメントする