函数文档

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_name are 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.

More Information

  • This function can be useful when your arguments are already in an array, and/or when there are many arguments to pass. Just make sure that your arguments are in the proper order!
  • Before PHP 5.4, your callback is passed a reference pointer to the array. Your callback can use this pointer to access all the array elements. Adding action and declaring a call back that hooks the above example action could look like this:

add_action('my_action', 'my_callback');
function my_callback( $args ) {
//access values with $args[0], $args[1] etc.
}

Because the array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.

  • Regardless of PHP version, you can specify the number of array elements when adding the action, and receive each element in a separate parameter in the callback function declaration like so:

  • add_action('my_action', 'my_callback', 10, 4 );
    function my_callback( $arg1, $arg2, $arg3, $arg4 ) {
    //access values with $args1, $args2 etc.
    }

    This method copies the array elements into the parameter variables. Any changes to the parameter variables do not affect the original array.

  • As of PHP 5.4, the array is no longer passed by reference despite the function’s name. You cannot even use the reference sign ‘&’ because call time pass by reference now throws an error. What you can do is pass the reference pointer as an array element. Doing so does require all callbacks added to the action to expect a reference pointer.

  • do_action_ref_array( 'my_action', array( &$args ) );
    
    add_action('my_action', 'my_callback');
    function my_callback( &$args ) {
    //access values with $args[0], $args[1] etc.
    }

    Because the original array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.

    If the array contains an object reference, the technique is as follows:

    do_action_ref_array( 'my_action', array( &$my_object ) );
    
    add_action('my_action', 'my_callback');
    function my_callback( $my_object ) {
    // $my_object->some_method()... etc.
    }

    The object’s properties can be changed. See the action ‘phpmailer_init‘ with the callback fix_phpmailer_messageid() in WordPress for an example.

    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.

    User Contributed Notes

    1. Skip to note 4 content

      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.
      }