I would like to use a function from class B as a second argument of the add_action() function of class A, here is what I tried for now (I simply created an instance of class B in class A):

class A

    public function __construct()


        include_once plugin_dir_path( __FILE__ ).'/classB.php';

        $classB= new classB();

        add_action('admin_menu', array($this, 'add_admin_menu'), 20);


    public function add_admin_menu()


            add_menu_page('Plugin Back End', 'Back End Plugin', 'manage_options', 'backend', array($this, 'menu_html'));

            $hook_heb = add_submenu_page('backend', 'hebergements', 'hebergements', 'manage_options', 'hebergements', array($this, 'menu_hebergements'));

            add_action('load-'.$hook_heb, array($this, 'process_action_heb'));




class Hebergement


    public function process_action_heb()


        if (isset($_POST['send_hebergement'])) {




In wordpress admin panel I get this error:

class 'Other_Part' does not have a method 'process_action_heb' in C:\xampp\htdocs\wordpress\wp-includes\class-wp-hook.php on line 298

Anyone as a solution for this problem ?

Much thanks


The problem is that you're trying to callback the process_action_heb method from $this (aka class A) and it's not a method of the A class - it's a method of B.

I'd use dependency injection to solve this, though you could use a method like you're using.

//* plugin.php
include_once PATH_TO . '/classA.php';
include_once PATH_TO . '/classB.php';

$classB = new classB();
$classA = new classA( $classB );

add_action( 'hookA', [ $classA, 'some_method' ], 20 );

//* class-a.php
class A {
  protected $classB;
  public function __construct( $classB ) {
    $this->classB = $classB;
  public function some_method() {
    //* Do something

    //* And add action to classB::some_method
    add_action( 'hookB', [ $this->classB, 'some_method' ] );

//* class-b.php
class B {
  public function some_method() {
    //* This code will run on the hookB hook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.