Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress和Woocommerce中的钩子及其钩子函数执行队列_Php_Wordpress_Woocommerce_Hook Woocommerce_Hook Wordpress - Fatal编程技术网

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

Php Wordpress和Woocommerce中的钩子及其钩子函数执行队列,php,wordpress,woocommerce,hook-woocommerce,hook-wordpress,Php,Wordpress,Woocommerce,Hook Woocommerce,Hook Wordpress,我是Wordpress/WooCommerce和PHP新手,尽管我在其他web平台和语言方面有经验。我已经搜索过了,但是没有找到我的问题的答案,那就是 由“add_action”创建的钩子是否添加到该特定钩子调用的操作列表中,或者它们是否覆盖该操作的任何现有钩子 例如,如果我添加一个woocommerce\u thankyouhook,使用: add_action( 'woocommerce_thankyou', 'order_created_get_skus',#); 问题:这是否会覆盖wo

我是Wordpress/WooCommerce和PHP新手,尽管我在其他web平台和语言方面有经验。我已经搜索过了,但是没有找到我的问题的答案,那就是

由“add_action”创建的钩子是否添加到该特定钩子调用的操作列表中,或者它们是否覆盖该操作的任何现有钩子

例如,如果我添加一个
woocommerce\u thankyou
hook,使用:

add_action( 'woocommerce_thankyou', 'order_created_get_skus',#);
问题:这是否会覆盖
woocommerce\u Thankyu
的任何其他钩子,或者除了为
woocommerce\u Thankyu
设置的任何其他钩子之外,还会调用它

钩子函数永远不会覆盖使用相同操作或过滤器钩子的其他钩子函数

它们被添加到一种“钩子队列”,执行顺序基于优先级规则:

  • 如果指定了优先级,它们将首先在队列中按钩子优先级和声明优先级排序
  • 如果没有指定优先级,则它们采用默认优先级10,并将在队列中按声明排序
因此,在同一个钩子上可以有许多钩子函数,例如在Woocommerce模板文件中

举例说明: 在下面的注释代码示例中,您可以在钩子队列中看到
woocommerce\u 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。

钩住的函数只能使用或使用强制定义的优先级删除

要查看特定钩子上有多少钩子函数,以及所有必要的详细信息,可以使用以下命令获得原始输出:

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>';
}
global$wp\u过滤器;
//在下面定义目标钩子名称
$hook_name='woocommerce_widget_shopping_cart_buttons';
if(isset($wp\U过滤器[$hook\U名称]){
回声';
打印($wp_filter[$hook_name]);
回声';
}否则{
echo“Hook”.$Hook_name.”尚未使用!

; }

正如您所注意到的,有两种钩子,即过滤器钩子和动作钩子

  • 动作钩:

    // 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]
    }
    
    • 动作挂钩执行点(触发器):带
    • 将函数附加到动作挂钩(已触发):with:执行该函数,并且可以有可选参数
  • 过滤器挂钩:

    • 过滤器挂钩执行点(触发器):带
    • 将函数附加到过滤器挂钩(过滤/触发):with:过滤强制参数(变量),并从“挂钩”函数返回
  • 钩子及其钩子函数可以位于任何位置,如活动子主题(或活动主题)的function.php文件中,也可以位于任何插件php文件中


    相关的:


    LoicTheAztec,非常感谢您的解释。