WP / WC 从另一个函数内部调用 add_action "init"
WP / WC calling add_action "init" from inside another function
我的插件和 WooCommerce 有问题。
所以我有一个带有选项页面的插件,上面有一个自定义复选框。
激活此复选框后,我想 hide/remove 默认的 WooCommerce 相关产品容器。
如果我只添加此代码,我可以删除此容器:
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
但问题是我需要从另一个 "add_filter" 函数中调用这个函数。
此刻我有这样的事情:
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
但是当我更改管理设置中的选项时,没有任何变化。所以我认为 "init" 挂钩不在此处。
我找不到合适的钩子来完成这项工作。当我想让它在插件选项更新时触发时,我必须使用什么钩子?
提前致谢,
莫
感谢 Danijel 和他的回答。
我不知道为什么我没有这样想。
也许在那个深夜对我来说 "action" 太多了 ;)
我现在将 "add_action" 放在 "add_filter" 函数之外,并在那里进行条件检查。
这是有效的:
add_action( 'init', 'hide_related');
function hide_related () {
if ( get_option( 'prfx_active', 'no' ) == 'yes' ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
};
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
...
尝试将您的 add_action_funciton
移到 add_filter_function
之外
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
我很确定 WP init
操作在 woocommerce_after_single_product_summary
过滤器之前触发,而且 if ( $active = 'yes' { ...
表达式将始终被评估为 true
(使用 ==
).试试这个简单的例子:
add_action( 'init', function() {
if ( get_option( 'prfx_active', 'no' ) == 'yes' )
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
});
我的插件和 WooCommerce 有问题。
所以我有一个带有选项页面的插件,上面有一个自定义复选框。
激活此复选框后,我想 hide/remove 默认的 WooCommerce 相关产品容器。
如果我只添加此代码,我可以删除此容器:
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
但问题是我需要从另一个 "add_filter" 函数中调用这个函数。
此刻我有这样的事情:
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
但是当我更改管理设置中的选项时,没有任何变化。所以我认为 "init" 挂钩不在此处。
我找不到合适的钩子来完成这项工作。当我想让它在插件选项更新时触发时,我必须使用什么钩子?
提前致谢, 莫
感谢 Danijel 和他的回答。
我不知道为什么我没有这样想。 也许在那个深夜对我来说 "action" 太多了 ;)
我现在将 "add_action" 放在 "add_filter" 函数之外,并在那里进行条件检查。
这是有效的:
add_action( 'init', 'hide_related');
function hide_related () {
if ( get_option( 'prfx_active', 'no' ) == 'yes' ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
};
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_filter_function () {
...
尝试将您的 add_action_funciton
移到 add_filter_function
add_filter( 'woocommerce_after_single_product_summary', 'add_filter_function' );
function add_action_function(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
}
function add_filter_function () {
// Get the plugin option
$active = get_option( 'prfx_active', 'no');
// If option value is "yes", remove the related products container
if ($active = 'yes') {
// I think this add_action call is wrong
add_action( 'init', 'add_action_function');
}//END if $active = yes
// Do some other stuff here
}//END add_filter_function
我很确定 WP init
操作在 woocommerce_after_single_product_summary
过滤器之前触发,而且 if ( $active = 'yes' { ...
表达式将始终被评估为 true
(使用 ==
).试试这个简单的例子:
add_action( 'init', function() {
if ( get_option( 'prfx_active', 'no' ) == 'yes' )
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
});