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,


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.

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.