- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/do action
提供: WordPress Codex 日本語版
説明
フックを作成します。
この関数は、アクションフック $tag に接続されているすべての関数を呼び出します。
これは、$tagパラメータを使用して、新しいフックの名前を指定してこの関数を呼び出し、新しいアクションフックを作成することが可能です。
フックに追加の引数を渡すことで、、 apply_filters()で多くのことができます。
この関数は、何も返さず、関数またはメソッドだけが呼び出されていることを除き、apply_filters() に似ています。
add_action でアクションが付加可能になります。
使い方
<?php do_action( $tag, $arg ); ?>
複数の引数:
<?php do_action( $tag, $arg_a, $arg_b, $etc ); ?>
パラメータ
- $tag
- (文字列) (必須) 作成したいフック名
- 初期値: なし
- $arg
- (mixed) (オプション) 引数のリストは、このフックに送信します。
- 初期値: 空の文字列
返り値
なし
用例
<?php # ======= (mu-)プラグイン、テーマ、コアのどこか ======= # /** * You can have as many arguments as you want, * but your callback function and the add_action call need to agree in number of arguments. * Note: `add_action` above has 2 and 'i_am_hook' accepts 2. * You will find action hooks like these in a lot of themes & plugins and in many place @core * @see: http://codex.wordpress.org/Plugin_API/Action_Reference */ # ======= 例えば functions.phpファイル内 ======= # /** * Define callback function * Inside this function you can do whatever you can imagine * with the variables that are loaded in the do_action() call above. */ function who_is_hook( $a, $b ) { echo '<code>'; print_r( $a ); // `print_r` the array data inside the 1st argument echo '</code>'; echo '<br />'.$b; // echo linebreak and value of 2nd argument } // then add it to the action hook, matching the defined number (2) of arguments in do_action // see [http://codex.wordpress.org/Function_Reference/add_action] in the Codex // add_action( $tag, $function_to_add, $priority, $accepted_args ); add_action( 'i_am_hook', 'who_is_hook', 10, 2 ); // Define the arguments for the action hook $a = array( 'eye patch' => 'yes' ,'parrot' => true ,'wooden leg' => (int) 1 ); $b = 'And Hook said: "I ate ice cream with Peter Pan."'; // Executes the action hook named 'i_am_hook' do_action( 'i_am_hook', $a, $b ); # ======= ブラウザで出力を参照 ======= # Array ( ['eye patch'] => 'yes' ['parrot'] => true ['wooden leg'] => 1 ) And hook said: "I ate ice cream with Peter Pan."
注
- Uses: グローバル変数 /en $wp_filter - フィルタとアクションのすべてを格納する。
- Uses: グローバル変数 /en $wp_actions - アクションがトリガーされた回数の量を増やします。
変更履歴
1.2.0 以降
ソースファイル
do_action() は wp-includes/plugin.php
にあります。
関連資料
アクション: has_action(), add_action(), do_action(), do_action_ref_array(), did_action(), remove_action(), remove_all_actions()
関数リファレンス、テンプレートタグ目次もご覧ください。
最新英語版: WordPress Codex » Function Reference/do_action (最新版との差分)