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 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

1 Answer 1

up vote 0 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 at 7:13

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.