filters – remove_action: how to access to a method in an child class?

Question

I’ve this in my main plugin file:

$main_admin_class = new MainAdminClass();
$main_admin_class->init();

MainAdminClass has this code:

class MainAdminClass {
    public function init() {
        $child_admin = new ChildAdminClass();
        $child_admin->init();
    }
}

and, finally, the child class has this code

class ChildAdminClass {
    public function init() {
        add_action('admin_notices', array($this, 'printFoo'));
        //I know it works if I use 'ChildAdminClass' instead of $this
    }

    public function printFoo() {
        echo 'fooooooooooooooooooooooooooooo';
    }
}

When later I try to use remove_action in functions.php, with this code:

global $childAdmin;
remove_action('admin_notices', array($child_admin, 'printFoo'));

It doesn’t works, unless I call the method statically.

I’m aware that it works if I use the add_action in the MainAdminClass instead of the child, but I would like to know if it is possible to make this works.

0
Dudo1985 4 weeks 2021-04-12T10:39:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse