doucaishou0074 2018-10-11 16:54
浏览 1048
已采纳

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

I am new to Wordpress/WooCommerce and PHP, although I have experience in other web platforms and languages. I have searched, but have not found the answer to my question which is...

Are hooks that are created by the "add_action" "added" to the list of actions called by that specific hook, or do they override any existing hooks of that action?

For example, if I add a woocommerce_thankyou hook using:

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

Question: Does this override any other hooks for woocommerce_thankyou or does it get called in addition to any other hooks set for woocommerce_thankyou?

  • 写回答

1条回答 默认 最新

  • doune1000 2018-10-11 19:54
    关注

    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

    Illustrated example:

    In the below commented code example, you can see the execution order in the hook queue for each hooked function for the woocommerce_thankyou action hook:

    // 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]
    }
    

    Lower priority is executed (or triggered) before, higher priority is executed (or triggered) after. If no priority is specified, the default priority is 10.

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

    To see how many hooked functions are hooked on a specific hook with all necessary details, you can use the following that will give you a raw output:

    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>';
    }
    

    There is 2 kind of hooks, as you have noticed may be, which are filter hooks and action hooks.

    1. Action hook:

      • Action hook execution point (trigger): with do_action()
      • Attaching a function to an action hook (triggered): with add_action(): the function is executed and can have optional arguments.
    2. Filter hook:

      • Filter hook execution point (trigger): with apply_filters()
      • Attaching a function to a filter hook (filtering / triggered): with add_filter(): a mandatory argument (a variable) is filtered and returned from the "hooked" function

    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.


    Related:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试