【问题标题】:remove_action not working for functions within a pluginremove_action 不适用于插件中的功能
【发布时间】:2016-05-02 15:50:58
【问题描述】:

我正在尝试使用 remove_action 来阻止插件的一部分运行 - 不要问我为什么 :-)。

插件内的功能是:

add_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );

我正在尝试通过以下方式删除它:

remove_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );

由于某种原因,它没有起到作用,尽管这通常适用于 Wordpress / WooCommerce。

有人能解释一下为什么会这样吗?我还尝试将我的功能与其他事物挂钩,例如

add_action( 'init', 'remove_it' );
function remove_it() {
remove_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 );
}

(插件代码:https://codedump.io/share/axGWwMwAH0vn/1/linzs-hook-not-working) 干杯,

林茨

已编辑:这个问题与上一个关于 remove_action 不起作用的问题不同,因为这与错误的优先级有关 - 而这个优先级在 30 时是正确的。

【问题讨论】:

  • 我也尝试使用 'plugins_loaded' 钩子而不是 init,但无济于事。 codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded
  • 您是否尝试过提高/降低remove_action 的优先级?不确定这是否可行,但可能值得一试。
  • 谢谢你的现场,丹。在发布我的帖子之前,我确实检查了该帖子,但没有任何建议有效。我使用了正确的优先级(30),并且我尝试连接到“plugins_loaded”。您还有其他想法吗?
  • 谢谢迈克尔麦克尼尔。我会试一试,但据我了解,删除操作需要与添加操作共享相同的优先级,在本例中为“30”。

标签: php wordpress woocommerce hook


【解决方案1】:

您需要全局访问类变量。请试试这个。

add_action( 'wp', 'remove_it' );
function remove_it() {
 global $WC_Product_Gallery_slider;
 remove_action( 'woocommerce_before_single_product_summary', array( $WC_Product_Gallery_slider, 'show_product_gallery' ), 30 );
}

【讨论】:

  • 普拉纳夫!看起来很棒,谢谢,我绝对认为它是正确的。但它似乎不起作用 - 我也尝试将它连接到“plugins_loaded”。还有其他想法吗?干杯,林茨
  • 考虑将add_action('init', 'remove_it'); 更改为add_action( 'wp', 'remove_it', 30 );
  • 哦,谢谢普拉纳夫。这似乎奏效了!我知道我们现在已经将它与 'wp' 而不是 'init' 挂钩,但了解我们为什么添加 30 会有所帮助。
  • 我也很想删除这个动作: add_action( 'wp', array( $this, 'setup_gallery_archives' ), 20 );遵循您的逻辑,这就是要去做的事情: add_action( 'wp', 'remove_it2', 20 );函数 remove_it2() { 全局 $WC_Product_Gallery_slider; remove_action('wp', 数组($this, 'setup_gallery_archives'), 20); }
  • @LinzDarlington 代码add_action( 'woocommerce_before_single_product_summary', array( $this, 'show_product_gallery' ), 30 ); 位于与wp 挂钩的函数中。 remove_action 仅适用于正确的优先级值。它必须与add_action 相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 2023-03-18
  • 2015-11-27
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多