【问题标题】:Use remove_action by accessing a class which was initiated under another class - PHP通过访问在另一个类下启动的类来使用 remove_action - PHP
【发布时间】:2020-02-25 19:41:33
【问题描述】:

我在 WordPress 插件中遇到过这样的情况 -

这是插件的父类-

class parent_class {
   public function __construct() {
      $this->initiate();
  }
   public static function init() {
        $instance = new parent_class ();
        return $instance;
  }

  public function initiate() {
        new child_class();
    }
}

子类在另一个文件中定义-

class child_class {
    public function __construct() {
       add_action( 'action_from_other_plugin', array( $this, 'function_i_want_to_remove' ), 32 );
    }
}

现在,我的目标是 remove_action 像这样的动作 -

remove_action( 'action_from_other_plugin', array( child_class::init(), 'function_i_want_to_remove' ), 3200 );

不幸的是,它给出了一个未定义函数的错误。在这种情况下我缺少什么?

【问题讨论】:

    标签: php wordpress class object


    【解决方案1】:

    您不需要为删除操作添加类。你可以这样做:

    remove_action( 'action_from_other_plugin', 'function_i_want_to_remove');
    

    【讨论】:

    • 我试过了,但不幸的是在这种情况下不起作用。
    • 奇怪。你从哪里调用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2011-03-27
    • 2019-05-28
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多