4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wordpressのdo_actionとadd_action

Posted at

はじめに

WordPressの中を追っていく上で、do_actionの所でどのように動いているのか分からなかったので調べたことをまとめさせていただきました。

do_action

wordpressの
wp-includes/plugin.php
に以下のように定義されています。


function do_action($tag, $arg = '') {

第一引数はタグで、

do_action( 'template_redirect' );

としてタグをtemplate_redirectとすれば、do_actionを実行した時にtemplate_redirectタグで追加された関数が実行されます。

add_action

do_action関数が実行された時に呼ばれる関数を定義します。

wordpressの
wp-includes/plugin.php
に以下のように定義されています。

add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) 

第二引数はdo_actionにフックする関数です。

add_action('template_redirect', 'redirect_canonical');

とすれば、do_action( 'template_redirect' );が呼ばれた時に、function redirect_canonicalが実行されます。

参考にしたサイト

4
1
0

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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?