当前浏览:首页 / WordPress函数 / do_action()

do_action( string $hook_name, mixed $arg )

调用已添加到动作钩子的回调函数

action


描述

此函数调用附加到动作挂钩$hook_name的所有函数。只需调用此函数,使用$hook_name参数指定新挂钩的名称,就可以创建新的动作钩子。

您可以向挂钩传递额外的参数,就像使用apply_filters()一样。

用法示例:

// 动作的回调函数.
function example_callback( $arg1, $arg2 ) {
    // (maybe) do something with the args.
}
add_action( 'example_action', 'example_callback', 10, 2 );

/*
 * 通过调用 'example_callback()' 函数触发动作
 * 钩住上面的 `example_action`
 *
 * - 'example_action' 是动作钩子.
 * - $arg1 和 $arg2 是传递给回调函数的附加参数.
$value = do_action( 'example_action', $arg1, $arg2 );

参数

$hook_name

(string)(必填) 要执行的动作的名称。

$arg

(mixed)(可选) 传递给连接到动作的函数的附加参数。默认为空。



源码

查看源码 官方文档


更新日志

版本描述
5.3.0通过将现有的...$arg参数添加到函数签名中,将其形参化。
1.2.0开始引入

使用示例

  • 示例1

    实例

    # ======= Somewhere in a (mu-)plugin, theme or the core ======= #
    
    /**
     * 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: https://codex.wordpress.org/Plugin_API/Action_Reference
     */
    
    # ======= e.g., inside your functions.php file ======= #
    
    /**
     * 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 wpdocs_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 [https://codex.wordpress.org/Function_Reference/add_action] in the Codex 
    
    // add_action( $tag, $function_to_add, $priority, $accepted_args );
    add_action( 'wpdocs_i_am_hook', 'wpdocs_who_is_hook', 10, 2 );
    
    // Define the arguments for the action hook
    $a = array(
    	'eye patch'  => 'yes',
    	'parrot'     => true,
    	'wooden leg' => 1
    );
    $b = __( 'And Hook said: "I ate ice cream with Peter Pan."', 'textdomain' ); 
    
    // Executes the action hook named 'i_am_hook'
    do_action( 'wpdocs_i_am_hook', $a, $b );
    

    结果:

    Array ( 
    	['eye patch'] => 'yes'
    	['parrot'] => true
    	['wooden leg'] => 1
    )
    And hook said: "I ate ice cream with Peter Pan."
    
  • 示例2

    BIG GOTCHA:

    调用do_action时,如果传入的$arg是一个具有单个对象的数组,则它将传入该obejct,而不是数组。但是,如果数组的单个项不是数组,则它不会执行相同的switcheroo

    例如,

    function my_callback( $should_be_an_array ){
       var_dump($should_be_an_array);
    }
    add_action( 'my_action', 'my_callback' );
    do_action( 'my_action', array(new stdclass()) );
    do_action( 'my_action', array( 'array_item_thats_not_an_object') );
    

    回显

    object(stdClass)[420]
    array (size=1)
      0 => string 'array_item_thats_not_an_object' (length=30)
    

    请注意,我们第一次传入一个包含stdclass的数组时,回调函数只接收stdclass,而不是数组

  • 示例3