在 WordPress / WooCommerce 中为挂钩函数设置正确的优先级
Set the right priority for a hooked function in WordPress / WooCommerce
如何在不连续试错的情况下为钩子函数找到正确的优先级?
我必须在我的产品页面中找到 "digital price"(precio digital)的正下方正常价格。
php代码是这样的:
add_action( 'woocommerce_single_product_summary', "show_digital_price", 10 );
如您所见,数字价格高于正常价格:https://www.editorialufv.es/catalogo/territory-inhabited/
我应该更改数字 10
并输入例如 11
或 12
。
我的问题:有没有一种不用试错法就能快速找出正确优先级数的方法?
在激活了 woocommerce_single_product_summary
挂钩的 content-single-product.php
模板文件中,您将看到:
<?php
/**
* woocommerce_single_product_summary hook.
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
?>
这为您提供了有关 WooCommerce 在此挂钩上使用的优先级的信息。
So you can use for your show_digital_price()
function a priority between 11
and 19
…
现在有时其他插件or/and一些主题可以使用其他一些优先级。在这种情况下,您可以使用这个小代码片段:
global $wp_filter;
echo '<pre>';
print_r( $wp_filter['woocommerce_single_product_summary'] );
echo '</pre>';
这将输出一个数组中的所有挂钩函数,其中包含已定义挂钩的使用优先级和接受参数(此处为 woocommerce_single_product_summary
操作挂钩).
下面是一个真实输出的例子:
WP_Hook Object (
[callbacks] => Array (
[5] => Array (
[woocommerce_template_single_title] => Array (
[function] => woocommerce_template_single_title
[accepted_args] => 1
)
)
[10] => Array (
[woocommerce_template_single_rating] => Array (
[function] => woocommerce_template_single_rating
[accepted_args] => 1
)
[woocommerce_template_single_price] => Array (
[function] => woocommerce_template_single_price
[accepted_args] => 1
)
)
[20] => Array (
[woocommerce_template_single_excerpt] => Array (
[function] => woocommerce_template_single_excerpt
[accepted_args] => 1
)
)
[30] => Array (
[woocommerce_template_single_add_to_cart] => Array (
[function] => woocommerce_template_single_add_to_cart
[accepted_args] => 1
)
)
[31] => Array (
[WC_Subscriptions_Synchroniser::products_first_payment_date] => Array (
[function] => WC_Subscriptions_Synchroniser::products_first_payment_date
[accepted_args] => 1
)
)
[40] => Array (
[woocommerce_template_single_meta] => Array(
[function] => woocommerce_template_single_meta
[accepted_args] => 1
)
)
[50] => Array (
[woocommerce_template_single_sharing] => Array (
[function] => woocommerce_template_single_sharing
[accepted_args] => 1
)
)
[60] => Array (
[000000007484d339000000001aaf1b88generate_product_data] => Array (
[function] => Array (
[0] => WC_Structured_Data Object (
[_data:WC_Structured_Data:private] => Array ( )
)
[1] => generate_product_data
)
[accepted_args] => 1
)
)
)
[iterations:WP_Hook:private] => Array ( )
[current_priority:WP_Hook:private] => Array ( )
[nesting_level:WP_Hook:private] => 0
[doing_action:WP_Hook:private] =>
)
正如您在此示例中看到的,Woocommerce 订阅插件添加了一个优先级为 31
的挂钩。
如何在不连续试错的情况下为钩子函数找到正确的优先级?
我必须在我的产品页面中找到 "digital price"(precio digital)的正下方正常价格。
php代码是这样的:
add_action( 'woocommerce_single_product_summary', "show_digital_price", 10 );
如您所见,数字价格高于正常价格:https://www.editorialufv.es/catalogo/territory-inhabited/
我应该更改数字 10
并输入例如 11
或 12
。
我的问题:有没有一种不用试错法就能快速找出正确优先级数的方法?
在激活了 woocommerce_single_product_summary
挂钩的 content-single-product.php
模板文件中,您将看到:
<?php
/**
* woocommerce_single_product_summary hook.
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_rating - 10
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
* @hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
?>
这为您提供了有关 WooCommerce 在此挂钩上使用的优先级的信息。
So you can use for your
show_digital_price()
function a priority between11
and19
…
现在有时其他插件or/and一些主题可以使用其他一些优先级。在这种情况下,您可以使用这个小代码片段:
global $wp_filter;
echo '<pre>';
print_r( $wp_filter['woocommerce_single_product_summary'] );
echo '</pre>';
这将输出一个数组中的所有挂钩函数,其中包含已定义挂钩的使用优先级和接受参数(此处为 woocommerce_single_product_summary
操作挂钩).
下面是一个真实输出的例子:
WP_Hook Object (
[callbacks] => Array (
[5] => Array (
[woocommerce_template_single_title] => Array (
[function] => woocommerce_template_single_title
[accepted_args] => 1
)
)
[10] => Array (
[woocommerce_template_single_rating] => Array (
[function] => woocommerce_template_single_rating
[accepted_args] => 1
)
[woocommerce_template_single_price] => Array (
[function] => woocommerce_template_single_price
[accepted_args] => 1
)
)
[20] => Array (
[woocommerce_template_single_excerpt] => Array (
[function] => woocommerce_template_single_excerpt
[accepted_args] => 1
)
)
[30] => Array (
[woocommerce_template_single_add_to_cart] => Array (
[function] => woocommerce_template_single_add_to_cart
[accepted_args] => 1
)
)
[31] => Array (
[WC_Subscriptions_Synchroniser::products_first_payment_date] => Array (
[function] => WC_Subscriptions_Synchroniser::products_first_payment_date
[accepted_args] => 1
)
)
[40] => Array (
[woocommerce_template_single_meta] => Array(
[function] => woocommerce_template_single_meta
[accepted_args] => 1
)
)
[50] => Array (
[woocommerce_template_single_sharing] => Array (
[function] => woocommerce_template_single_sharing
[accepted_args] => 1
)
)
[60] => Array (
[000000007484d339000000001aaf1b88generate_product_data] => Array (
[function] => Array (
[0] => WC_Structured_Data Object (
[_data:WC_Structured_Data:private] => Array ( )
)
[1] => generate_product_data
)
[accepted_args] => 1
)
)
)
[iterations:WP_Hook:private] => Array ( )
[current_priority:WP_Hook:private] => Array ( )
[nesting_level:WP_Hook:private] => 0
[doing_action:WP_Hook:private] =>
)
正如您在此示例中看到的,Woocommerce 订阅插件添加了一个优先级为 31
的挂钩。