Add a sub menu page.
NOTE: If you're running into the "You do not have sufficient permissions to access this page." message in a wp_die() screen, then you've hooked too early. The hook you should use is admin_menu.
This function takes a capability which will be used to determine whether or not a page is included in the menu.
The function which is hooked in to handle the output of the page must check that the user has the required capability as well.
This function should normally be hooked in with one of the the admin_menu actions depending on the menu where the sub menu is to appear:
| admin_menu | The normal, or site, administration menu |
| user_admin_menu | The user administration menu |
| network_admin_menu | The network administration menu |
<?php add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); ?>
add_submenu_page(), such as the $page_title. Typically, these will work:
get_admin_page_parent()
get_admin_page_title(), or simply global $title
global $plugin_page
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page( 'tools.php', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-custom-submenu-page', 'my_custom_submenu_page_callback' );
}
function my_custom_submenu_page_callback() {
echo '<div class="wrap"><div id="icon-tools" class="icon32"></div>';
echo '<h2>My Custom Submenu Page</h2>';
echo '</div>';
}
To hide your submenu link from a top level menu item to which it belongs you would instead do,
add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
add_submenu_page(
null //or 'options.php'
, 'My Custom Submenu Page'
, 'My Custom Submenu Page'
, 'manage_options'
, 'my-custom-submenu-page'
, 'my_custom_submenu_page_callback'
);
}
If you are attempting to add a submenu page to a menu page created via add_menu_page() the first submenu page will be a duplicate of the parent add_menu_page().
If you want a submenu page in this scenario, you should first create a duplicate of your add_menu_page() and then add your add_submenu_page():
add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
add_submenu_page( 'my-top-level-slug', 'My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug');
add_submenu_page( 'my-top-level-slug', 'My Custom Submenu Page', 'My Custom Submenu Page', 'manage_options', 'my-secondary-slug');
add_submenu_page() is located in wp-admin/includes/plugin.php.
Administration Menus: add_menu_page(), remove_menu_page(), add_submenu_page(), remove_submenu_page(), add_dashboard_page(), add_posts_page(), add_media_page(), add_links_page(), add_pages_page(), add_comments_page(), add_theme_page(), add_plugins_page(), add_users_page(), add_management_page(), add_options_page()