在Wordpress和Woocommerce中,钩子及其钩子函数执行队列 [英] Hooks and their hooked functions execution queue in Wordpress and Woocommerce

查看:488
本文介绍了在Wordpress和Woocommerce中,钩子及其钩子函数执行队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Wordpress/WooCommerce和PHP还是陌生的,尽管我在其他Web平台和语言方面也有经验.我已经搜索过,但是没有找到我的问题的答案.

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...

由"add_action"添加"创建的钩子是否存在于由该特定钩子调用的动作列表中,或者它们会覆盖该动作的任何现有钩子?

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?

例如,如果我使用以下方法添加woocommerce_thankyou钩子:

For example, if I add a woocommerce_thankyou hook using:

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

问题:这是否覆盖了woocommerce_thankyou的任何其他钩子,还是除了为woocommerce_thankyou设置的任何其他钩子之外还被调用?

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?

推荐答案

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

将它们添加到一种挂钩队列" 中,并具有基于优先级规则的执行顺序:

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

  • 如果指定了优先级,则将首先根据钩子优先级和声明优先级对它们进行排序.
  • 如果未指定优先级,则它们的默认优先级为10,并将通过声明在队列中进行排序.

因此,您可以在同一个钩子上具有许多钩子函数,例如在Woocommerce模板文件

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 操作钩子的每个钩子函数的钩子队列中看到执行顺序:

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

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

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

只能通过> remove_action() 删除挂钩的函数>或具有强制性定义优先级的 remove_filter() .

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. 动作挂钩:

  1. Action hook:

  • 动作挂钩执行点:(触发):带有 add_action() :该函数已执行,并且可以具有可选参数.
  • 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.

过滤器挂钩:

  • 过滤器挂钩执行点:(触发):带有 add_filter() :强制参数(变量)被过滤并从挂钩"函数中返回
  • 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

钩子及其钩子函数可以位于任何位置,例如您的活动子主题 (或活动主题)的function.php文件中,以及任何插件 php文件.

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?

这篇关于在Wordpress和Woocommerce中,钩子及其钩子函数执行队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆