WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying for the first time to build a plugin for Wordpress following OOP way. I am having some problems while trying to create a submenu page using add_users_page() function.

Here is a simple version of my code :

class MyPlugin  {
    public function __construct()
       add_action('admin_menu', array($this, 'create_menu'));

    public static function create_menu()
    add_users_page( 'My Plugin Menu', 'My Plugin Menu (Test)', 'list_users', 'mp_test_page', array($this, 'display_test_page'));

    public static function display_test_page()
    if ( !current_user_can( 'list_users' ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    echo '<h2>Test page</h2>';
    echo '<p>The Problem : I cannot see that output in the corresponding submenu page</p>';
new MyPlugin();

The problem is that I cannot see the html output in the display_test_page() function... although the submenu page was indeed created in the Users general menu.

Thank you in advance for your help,

share|improve this question
up vote 1 down vote accepted

You have mix up in static and non-static usage. Your style is non-static, but your methods are declared as such. If you enable WP_DEBUG mode you'll see following errors:

Notice: Undefined variable: this

Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object

This happens because static method doesn't "know" what $this refers to, since static isn't tied to specific instance of the class.

Simply declare your methods as public rather than public static for this code organization.

share|improve this answer
Thank you for your help :-) – user54952 Jul 1 '14 at 7:13

Your Answer


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.