admin管理员组

文章数量:1201993

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().

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().

Share Improve this question asked May 9, 2022 at 8:09 FoysalFoysal 4451 gold badge5 silver badges16 bronze badges 1
  • 1 I don't see how/where/when the parent menu (sports_info?) is added, but make sure that you used the correct parent slug. Or did you mean to add a top-level menu instead, e.g. using add_menu_page()? – Sally CJ Commented May 9, 2022 at 9:02
Add a comment  | 

1 Answer 1

Reset to default 1

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

本文标签: pluginsUse current class method inside addsubmenupage()