plugins - Adding an admin page - OOP approach not working

I have a parent class, which contains the following:use appMy_Dashboard;class My_Class {public function __construct() {

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.

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.

Share Improve this question edited Sep 1, 2015 at 16:02 Rutwick Gangurde asked Sep 1, 2015 at 14:48 Rutwick GangurdeRutwick Gangurde 8,6045 gold badges42 silver badges55 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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 ;)

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'));

You can simply add your menu page like this, I hope it's a simple approach

class MyPlugin{

    function __construct()
    {
        add_action('admin_menu', array($this, 'add_menu_page_fun'));
    }

    // Add Menu Page
    function add_menu_page_fun() {        
        add_menu_page(
            __('My Plugin Page','myplugin-menupage'), //Page title
            __('My Plugin Title','myplugin-menu'), //Menu title
            'manage_options', //capability
            'my-plug-handle', //menu_slug
            array($this, 'toplevel_page'), //function
            'dashicons-buddicons-activity' //icon url
        );
    }
    
    // Displays the page content for the custom Toplevel menu
    function toplevel_page() {
        // echo "<h2>" . __( 'Welcome to admin page', 'myplugin-menu' ) . "</h2>";
        include 'My-admin.php'; //new php file
    }

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742299563a4417782.html

相关推荐

  • plugins - Adding an admin page - OOP approach not working

    I have a parent class, which contains the following:use appMy_Dashboard;class My_Class {public function __construct() {

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信