do_action_ref_array()
云策文档标注
概述
do_action_ref_array() 是 WordPress 中用于执行动作钩子的函数,与 do_action() 类似,但通过数组传递参数。它适用于参数已为数组或参数较多的情况,需注意参数顺序和 PHP 版本差异对引用传递的影响。
关键要点
- 函数功能:调用已添加到动作钩子的回调函数,参数以数组形式提供。
- 参数说明:$hook_name(字符串,必需)为动作名称;$args(数组,必需)为传递给钩子函数的参数。
- PHP 版本差异:PHP 5.4 之前,数组通过引用传递,回调函数可修改原数组;PHP 5.4 及之后,默认不再通过引用传递,需显式传递引用指针。
- 使用场景:当参数已为数组或参数数量多时,使用此函数可简化代码。
- 回调函数声明:可通过 add_action() 指定参数数量,在回调中接收独立参数,此时数组元素被复制到参数变量,修改不影响原数组。
代码示例
// 基本用法:传递数组参数
do_action_ref_array( 'my_action', array( 'arg_1', true, 'foo', 'arg_4' ) );
// 与 do_action() 等效
do_action( 'my_action', 'arg_1', true, 'foo', 'arg_4' );
// 引用传递示例(PHP 5.4 前或显式传递)
do_action_ref_array( 'my_action', array( &$args ) );
add_action( 'my_action', 'my_callback' );
function my_callback( &$args ) {
// 通过 $args[0], $args[1] 访问值
}
// 指定参数数量的回调
do_action_ref_array( 'wpdocs_my_action', array( &$arg1, &$arg2, &$arg3, &$arg4 ) );
add_action( 'wpdocs_my_action', 'wpdocs_my_callback', 10, 4 );
function wpdocs_my_callback( &$arg1, &$arg2, &$arg3, &$arg4 ) {
// 访问和修改参数值
}注意事项
- 确保参数数组的顺序与回调函数期望的参数顺序一致。
- 在 PHP 5.4 及以上版本中,do_action_ref_array() 默认不通过引用传递数组,需使用 & 符号显式传递引用指针。
- 如果数组包含对象引用,回调函数可以修改对象的属性。
- 参考 WordPress 中的 phpmailer_init 动作和 fix_phpmailer_messageid() 回调作为实际应用示例。
原文内容
Calls the callback functions that have been added to an action hook, specifying arguments in an array.
Description
See also
- do_action(): This function is identical, but the arguments passed to the functions hooked to
$hook_nameare supplied using an array.
Parameters
$hook_namestringrequired-
The name of the action to be executed.
$argsarrayrequired-
The arguments supplied to the functions hooked to
$hook_name.
Source
function do_action_ref_array( $hook_name, $args ) {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
$wp_actions[ $hook_name ] = 1;
} else {
++$wp_actions[ $hook_name ];
}
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
}
$wp_filter[ $hook_name ]->do_action( $args );
array_pop( $wp_current_filter );
}
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 3 content
Codex
Basic Example
Call an action and pass an array of arguments:
$args = array( 'arg_1', true, 'foo', 'arg_4' ); do_action_ref_array( 'my_action', $args );This is the same as:
do_action( 'my_action', 'arg_1', true, 'foo', 'arg_4' );Skip to note 4 content
Chad Cloman
Here’s a variation on the “separate parameters” example given above. It allows you to specify separate parameters -and- modify them:
Hook:
do_action_ref_array( 'wpdocs_my_action', array( &$arg1, &$arg2, &$arg3, &$arg4 ) );Callback:
add_action( 'wpdocs_my_action', 'wpdocs_my_callback', 10, 4 ); function wpdocs_my_callback( &$arg1, &$arg2, &$arg3, &$arg4 ) ) { // access and modify values with $args1, $args2 etc. }