admin管理员组

文章数量:1122832

Sorry if this has been asked before; I have been looking for a while, but couldn't find anything directly applicable. I am building my first plugin and I am trying to follow the WP best practices.

My current plugin structure looks like this:

- my-plugin
-- index.php
-- admin (folder
--- sub-menu-page.php

I am trying to keep all of my plugin pages separate as part of my organization. However, I can't seem to point my plugin's submenu link to the sub-menu-page.php file. Here's my menu code:

add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
    add_menu_page('WISE Admin', 'WISE Admin', 'manage_options', 'wise-menu', 'my_menu_output', 'dashicons-tickets', 2 );
    add_submenu_page('wise-menu', 'General Settings', 'General', 'manage_options', 'general-settings', 'general_settings' );
    add_submenu_page('wise-menu', 'Submenu Page', 'Submenu Page', 'manage_options', 'admin/sub-menu-page' );
    remove_submenu_page('wise-menu', 'wise-menu');
}

The submenu link takes me to /wp-admin/admin/sub-menu-page which is obviously not where I want to go.

Can I please have some advice for what I am doing wrong?

Sorry if this has been asked before; I have been looking for a while, but couldn't find anything directly applicable. I am building my first plugin and I am trying to follow the WP best practices.

My current plugin structure looks like this:

- my-plugin
-- index.php
-- admin (folder
--- sub-menu-page.php

I am trying to keep all of my plugin pages separate as part of my organization. However, I can't seem to point my plugin's submenu link to the sub-menu-page.php file. Here's my menu code:

add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
    add_menu_page('WISE Admin', 'WISE Admin', 'manage_options', 'wise-menu', 'my_menu_output', 'dashicons-tickets', 2 );
    add_submenu_page('wise-menu', 'General Settings', 'General', 'manage_options', 'general-settings', 'general_settings' );
    add_submenu_page('wise-menu', 'Submenu Page', 'Submenu Page', 'manage_options', 'admin/sub-menu-page' );
    remove_submenu_page('wise-menu', 'wise-menu');
}

The submenu link takes me to /wp-admin/admin/sub-menu-page which is obviously not where I want to go.

Can I please have some advice for what I am doing wrong?

Share Improve this question edited Apr 3, 2019 at 8:41 MikeNGarrett 2,6611 gold badge20 silver badges29 bronze badges asked Mar 5, 2019 at 16:44 user1192309user1192309 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're very close.

When you add your first submenu page you want to duplicate your initial menu page. In your second add_submenu_page you are missing the function callback for that page. Technically this parameter is optional, but it changes your url from /wp-admin/sub-menu-page to /wp-admin/admin.php?page=sub-menu-page.

Give this a shot:

add_action( 'admin_menu', 'my_menu_pages' );
function my_menu_pages() {
    add_menu_page( 'WISE Admin', 'WISE Admin', 'manage_options', 'wise-menu', 'my_menu_output', 'dashicons-tickets', 2 );
    add_submenu_page( 'wise-menu', 'General Settings', 'General', 'manage_options', 'wise-menu', 'my_menu_output' );
    add_submenu_page( 'wise-menu', 'Submenu Page', 'Submenu Page', 'manage_options', 'sub-menu-page', 'submenu_settings' );
}

本文标签: Plugin submenu pages recommended structure and links