Hooks and their hooked functions execution queue in Wordpress and Woocommerce

别说谁变了你拦得住时间么 提交于 2019-12-08 04:32:10

问题


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:


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:

  • WooCommerce action hooks and overriding templates
  • How to add custom hooks to a custom plugin for Woocommerce
  • WordPress: Difference Between Filter and Action Hooks?


来源:https://stackoverflow.com/questions/52765303/hooks-and-their-hooked-functions-execution-queue-in-wordpress-and-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!