2

I have a parent class, which contains the following:

use app\My_Dashboard;

class My_Class {
    public function __construct() {
        add_action('admin_menu', array($this, 'setup_dashboard_page'));
    }

    public function setup_dashboard_page() {
        My_Dashboard::add_dashboard_page();
    }

}

And the class which has the admin page functions:

class My_Dashboard {
    public static function add_dashboard_page() {
        add_menu_page('My Settings', 'My Settings', 'my-settings', array($this, 'dashboard_page_cb'));
    }

    public function dashboard_page_cb() {
        //The markup
    }
}

This approach doesn't work, and I cannot see my admin menu item and page added. The second approach I tried:

use app\My_Dashboard;

class My_Class {
    public function __construct() {
        add_action('admin_menu', array($this, 'setup_dashboard_page'));
    }

    public function setup_dashboard_page() {
        //Not very sure of the syntax, bad at OOP
        add_menu_page('My Settings', 'My Settings', 'my-settings', 'My_Dashboard::dashboard_page_cb');
    }
}

How do I get it to work? My idea is to separate all the dashboard related stuff to the other class.

Update: The first approach worked, and the menu item is added. But the callback function that outputs the page markup doesn't seem to work. I checked for typos but everything seems okay.

3

I hate OOP mixed in the functional structure of wordpress (here come the downvotes.....). If you use OOP just for name spacing then just use the name spacing feature of PHP, putting function in a class is just a very small part of what OOP should be.

Anyway, your problems is that a there are only two way to refere to an class method, either when it is associated with a specific object or if it is static in the class (basically associated with the class but not with the objects). In your code you try to treat an object bound function as if it is a static one (I am sure that if you will check your error log you will see some relevant error). There are two possible way to fix it 1. Make the relevant functions static and use the syntax in your second variation 2. use a singleton patter, instantiate an object of the dashboard class, keep it as a global and use it instead of the class name in your first variation. instead of

public function setup_dashboard_page() {
    My_Dashboard::add_dashboard_page();
}

do

global $dashboard;
$dashboard = new my_dashboard()
.....
public function setup_dashboard_page() {
   global $dashboard;
    $dashboard->add_dashboard_page();
}

And always debug code with full error reporting and fix all errors and notices ;)

  • Thanks! I managed to follow a static method approach and get it to work, but the function that outputs the page content doesn't seem to work. I will update the code in my question. – Rutwick Gangurde Sep 1 '15 at 15:58
  • it is still exactly the same problem. the syntax array($object,'method name') needs an object as the first parameter but there is no $this object in static methods. – Mark Kaplun Sep 2 '15 at 1:30
2

Working Example

class PluginSkeleton {

     public function __construct(){
          add_action('admin_menu',array($this,'pluginskeleton_menu'));
     }
     public function pluginskeleton_menu(){
         add_menu_page( 'Application Users', 'Application Users', 'manage_options', 'application-users.php',array($this,'application_users_page'));
     }

    public function application_users_page(){
          echo 'i am here';
     }

}

use $this with callback function

add_menu_page( 'Application Users', '...', ...', '...',array($this,'application_users_page'));

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.