钩子及其钩子函数在 Wordpress 和 Woocommerce 中的执行队列

Hooks and their hooked functions execution queue in Wordpress and Woocommerce

我是 Wordpress/WooCommerce 和 PHP 的新手,尽管我有使用其他网络平台和语言的经验。我已经搜索过,但没有找到我的问题的答案...

"add_action" "added" 创建的挂钩是否指向特定挂钩调用的操作列表,或者它们是否覆盖该操作的任何现有挂钩?

例如,如果我添加一个 woocommerce_thankyou 挂钩,使用:

add_action( 'woocommerce_thankyou', 'order_created_get_skus',#);

问题: 这是否会覆盖 woocommerce_thankyou 的任何其他挂钩,或者除了为 woocommerce_thankyou 设置的任何其他挂钩之外还会调用它?

Hooked functions will never override other hooked functions that are using the same action or filter hook.

They are added to a kind of "hook queue" with an execution order based on priority rules:

  • If a priority is specified, they will be ordered in the queue first by hook priority and by declaration priority.
  • If there is no priority specified, they take the default priority of 10 and they will be ordered in the queue by declaration.

So you can have many hooked functions on the same hook, like for example in the Woocommerce template file content-single-product.php

图示示例:

在下面注释的代码示例中,您可以看到 woocommerce_thankyou 操作挂钩的每个挂钩函数在挂钩队列中的执行顺序:

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'first_custom_function_no_priority' );
function first_custom_function_no_priority( $order_id ) {
    // ==> Triggered in third position ==> [3]
}

## Default Hook "woocommerce_order_details_table" (default priority is 10)
    // ==> Triggered in second position ==> [2]

// Defined priority is 10
add_action( 'woocommerce_thankyou', 'order_created_get_skus', 10 );
function order_created_get_skus( $order_id ) {
    // ==> Triggered in Fourth position ==> [4] 
}

// Defined priority is 5
add_action( 'woocommerce_thankyou', 'third_custom_function', 5 );
function third_custom_function( $order_id ) {
    // ==> Triggered in first position ==> [1]
}

// Defined priority is 20
add_action( 'woocommerce_thankyou', 'fourth_custom_function', 20 );
function fourth_custom_function( $order_id ) {
    // ==> Triggered at last (sixth) ==> [6]
}

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'last_custom_function_no_priority' );
function last_custom_function_no_priority( $order_id ) {
    // ==> Triggered in fifth position ==> [5]
}

低优先级在(或触发)之前执行,高优先级在(或触发)之后执行。如果未指定优先级,默认优先级为 10。

The hooked functions can only be removed with remove_action() or remove_filter() with a mandatory defined priority.

要查看特定钩子上钩了多少个钩子函数以及所有必要的详细信息,您可以使用以下将为您提供原始输出的方法:

global $wp_filter;

// HERE below you define the targeted hook name
$hook_name = 'woocommerce_widget_shopping_cart_buttons';

if( isset($wp_filter[$hook_name]) ) {
    echo '<pre>';
    print_r($wp_filter[$hook_name]);
    echo '</pre>';
} else {
    echo '<p>Hook "'.$hook_name.'" is not used yet!</p>';
}

有 2 种挂钩,您可能已经注意到,它们是过滤器挂钩和操作挂钩。

  1. 动作挂钩:

    • Action hook执行点(触发):同do_action()
    • Attaching a function to an action hook (triggered): with add_action(): function is executed and can have可选参数。
  2. 过滤挂钩:

    • Filter hook执行点 (触发): with apply_filters()
    • 将函数附加到过滤器挂钩 (过滤/触发):使用add_filter():强制参数(一个变量)从"hooked"函数
    • 过滤返回

Hooks and their hooked functions can be located anywhere like in the function.php file of your active child theme (or active theme) and also in any plugins php files.


相关: