【问题标题】:Wordpress - remove_action from plugin classWordpress - 从插件类中删除操作
【发布时间】:2021-10-24 09:55:00
【问题描述】:

在 WordPress 中,如何删除插件类中的以下操作?

插件包括这个(我只是引用它的几行):

class WC_Product_Vendors_Vendor_Admin {
    public static function init() {
        self::$self = new self();
        add_action( 'pre_get_users', array( self::$self, 'filter_users' ) );
    }
}
WC_Product_Vendors_Vendor_Admin::init();

在我自己的自定义插件中,我尝试了:

function remove_my_class_action() {
    remove_action ( 'pre_get_users', array( WC_Product_Vendors_Vendor_Admin::$instance , 'filter_users') );
}
add_action( 'plugins_loaded', 'remove_my_class_action' );

但是 Wordpress 甚至不允许我保存它——它说找不到该类。

我也试过getInstance()而不是$instance

我还尝试了'wp_head''init',而不是'plugins_loaded'

还在 add_action 和 remove_action 上尝试了不同的优先级,但这并没有什么不同。 (虽然可能是我错过了上述的一些组合。)

【问题讨论】:

  • 不幸的是,没有,我尝试按照那里的答案实例化该类,但它仍然告诉我Uncaught Error: Class 'WC_Product_Vendors_Vendor_Admin' not found in wp-content/plugins/mycustomplugin/custom-roles.php:107
  • 你可以检查class_exisits ...我会假设供应商管理类是一个管理功能?
  • 单独 class_exists 返回 false ...如果我将 remove_action 放在带有 if (class_exists(...) 的条件中,我至少可以保存文件,但随后网站显示错误:@ 987654334@

标签: wordpress woocommerce


【解决方案1】:

尝试使用具有更高优先级的相同钩子(默认为 10)。该类中也不存在静态属性$instance。 在您从插件粘贴的代码中,您可以看到在 init 函数中它使用 $self 静态属性来存储实例。

function remove_my_class_action() {
    remove_action ( 'pre_get_users', array( WC_Product_Vendors_Vendor_Admin::$self , 'filter_users') );
}
add_action( 'pre_get_users', 'remove_my_class_action', 9 );

【讨论】:

  • 这完成了工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
相关资源
最近更新 更多