Use current class method inside add_submenu_page()

I have a class like below.

namespace Inc\Admin;

class Admin
{
    public function __construct()
    {
        add_action('admin_menu', [$this, 'admin_menu']);
    }

    public function admin_menu()
    {
       add_submenu_page('sports_info', 'Sports Information', 'Sports Information', 'manage_options', [$this,'sports_info_page'], [$this, 'html_page']);
    }

    public function html_page() {
       //some code
    }

    public function sports_info_page() {
      //some code
    }

}

I am getting below errors.

Fatal error: Uncaught Error: Object of class Inc\Admin\Admin could not be converted to string

Error comes from [$this,'sports_info_page'] of add_submenu_page().

Topic sub-menu plugins Wordpress

Category Web


You're getting that error because the 5th parameter for add_submenu_page() should be a string which is the menu slug and yet you supplied a callback ([$this,'sports_info_page']). So be sure to use a valid slug like so and the error will be gone:

add_submenu_page(
    'sports_info',             // parent slug
    'Sports Information',      // page title
    'Sports Information',      // menu title
    'manage_options',          // capability
    'your-menu-slug',          // menu slug
    [$this,'sports_info_page'] // callback
);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.