- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
「関数リファレンス/do action」の版間の差分
提供: WordPress Codex 日本語版
< 関数リファレンス
(翻訳済み、div は付けていない) |
(和訳の調整。) |
||
(2人の利用者による、間の9版が非表示) | |||
1行目: | 1行目: | ||
== 説明 == | == 説明 == | ||
− | + | 指定されたアクションフックに付けられた関数を実行します。 | |
+ | |||
+ | この関数はアクションフック <tt>$tag</tt> に付けられたすべての関数を実行します。この関数を、新しいフックの名前を <tt>$tag</tt> パラメータに指定して呼び出せば、アクションフックを新しく作成できます。<tt>[[関数リファレンス/apply_filters|apply_filters()]]</tt> と同様に、追加の引数をフックへ渡すことができます。この関数は、何も返さず、関数またはメソッドを呼び出すだけであることを除き、<tt>apply_filters()</tt> に似ています。 | ||
+ | |||
+ | <tt>[[関数リファレンス/add_action|add_action()]]</tt> を使って関数をアクションフックへ付けることができます。 | ||
== 使い方 == | == 使い方 == | ||
− | <?php do_action( $tag, $arg ); ?> | + | <?php do_action( $tag, $arg ); ?> |
+ | |||
+ | 複数の引数を与える場合: | ||
+ | <?php do_action( $tag, $arg_a, $arg_b, $etc ); ?> | ||
== パラメータ == | == パラメータ == | ||
− | {{Parameter|$tag|文字列| | + | {{Parameter|$tag|文字列|実行したいアクションフックの名前。}} |
− | {{Parameter|$arg| | + | {{Parameter|$arg|mixed|フックに付けられた関数へ送る引数のリスト。|オプション|空の文字列}} |
+ | |||
+ | == 戻り値 == | ||
+ | |||
+ | この関数は値を返しません。 | ||
== 用例 == | == 用例 == | ||
− | == | + | <pre> |
+ | <?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." | ||
+ | </pre> | ||
+ | |||
+ | == 参考 == | ||
+ | |||
+ | * [[グローバル変数]] /[[:en:Global_Variables |en]] <tt>$wp_filter</tt> を使用します。フィルターとアクションをすべて格納しています。 | ||
+ | * [[グローバル変数]] /[[:en:Global_Variables |en]] <tt>$wp_actions</tt> を使用して、アクションが呼び出された回数を数えます。<!-- Increments the amount of times the action was triggered.--> | ||
== 変更履歴 == | == 変更履歴 == | ||
− | 1.2.0 | + | 新規導入: [[Version_1.2|1.2.0]] |
== ソースファイル == | == ソースファイル == | ||
24行目: | 94行目: | ||
<tt>do_action()</tt> は {{Trac|wp-includes/plugin.php}} にあります。 | <tt>do_action()</tt> は {{Trac|wp-includes/plugin.php}} にあります。 | ||
− | + | == 関連項目 == | |
− | + | ||
− | == | + | |
{{Action Tags}} | {{Action Tags}} | ||
{{Tag Footer}} | {{Tag Footer}} | ||
+ | |||
+ | {{原文|Function Reference/do_action|143652}} <!-- 23:59, 17 May 2014 Hissy 版 --> | ||
{{DEFAULTSORT:Do_action}} | {{DEFAULTSORT:Do_action}} | ||
− | [[Category: | + | [[Category:関数]] |
+ | |||
+ | [[en:Function_Reference/do_action]] | ||
+ | [[fax:Function_Reference/do_action]] | ||
+ | [[ru:Справочник по функциям/do_action]] |
2015年8月14日 (金) 01:16時点における最新版
説明
指定されたアクションフックに付けられた関数を実行します。
この関数はアクションフック $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."
参考
- グローバル変数 /en $wp_filter を使用します。フィルターとアクションをすべて格納しています。
- グローバル変数 /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 (最新版との差分)
fax:Function_Reference/do_action