Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I thought I was making headway in my WordPress Development education until I ran across the Boilerplate for WordPress plugins and it uses a class object. I'm fine with that and understand those concepts and I am a .Net developer. But, for some reason, I cannot get any callback to function properly. All I want at this point is an admin menu and when you click on that menu item, it display something like "PLEASE HELP ME!!!". Check out what I have below and can anyone please explain what I have wrong?

class school_manager {
    function __construct() {
        add_action( 'init', array( $this, 'init' ) );
    }

    public function init(){
            add_action('admin_menu', array(&$this, 'add_admin_menu'));
        }

    public function add_admin_menu(){
        add_menu_page( 'School Manager Settings', 'School Manager', 'administrator', 'school-manager-settings', 'show_admin_settings_page', null ); 
    }

    public function show_admin_settings_page(){
        echo '<p>This is a test for the admin settings page.  Please work!</p>';
    }
}

My new menu item does appear (as it did when I was not using a class object in my plugin architecture. But, when I click on the menu item, I get the following error:

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'school_manager::create_admin_page' was given in H:\root\home\xxxxx\wp-includes\plugin.php on line 406

I am trying to create a plugin that has not only an admin menu and submenu items, but also want to utilize the Settings API by trying to follow this series. So, this is one of the crucial points to get something as simple as a menu item to function before I can continue.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

The callback you have given in add_menu_page() is a static function, not a class method. It should be:

add_menu_page( 
    'School Manager Settings', 
    'School Manager', 
    'administrator', 
    'school-manager-settings', 
    array ( $this, 'show_admin_settings_page' )
);

And please don't use &$this anymore, that's PHP 4. :)

My demo plugin T5 Admin Menu Demo might help understanding how that works.

share|improve this answer
    
Thanks so much for the reply. Your demo is great and well documented. The only thing I noticed different is that you are calling the admin_menu action outside the class definition. Will that matter if I have it inside or outside the class? If not, I would prefer to keep in inside my init method. –  ClockwiseQ Mar 29 '13 at 16:26
    
@ClockwiseQ In my demo I don't need to call the class earlier than on admin_menu. If you need it you class instance earlier, do it. Just don't call your class on every request (for example on front end or AJAX requests, a common mistake). –  toscho Mar 29 '13 at 16:30
    
+1 & a side question @toscho: I have so far always been going with &$this to ensure PHP4 compatibility, as well as analogously having a dummy constructor method with the same name as the parent class that in turn calls $this->__construct();. You reckon that's bullshit to do nowadays? –  Johannes Pille Mar 29 '13 at 20:05
1  
@JohannesPille WordPress does not work at all with PHP 4. Your plugin or theme code will never be called in PHP 4, so there is no point trying to be compatible. –  toscho Mar 29 '13 at 20:08
    
@toscho No kidding, huh. I will lighten up a ton of my classes in the near future I suppose... Thx. –  Johannes Pille Mar 29 '13 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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