add_submenu_page

Add a submenu page.

Description

(false|string) add_submenu_page( (string) $parent_slug, (string) $page_title, (string) $menu_title, (string) $capability, (string) $menu_slug, (string) $function = '' ); 

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.

Returns (false|string)

The resulting page's hook_suffix, or false if the user does not have the capability required.

Parameters (6)

0. $parent_slug (string)
The slug name for the parent menu (or the file name of a standard WordPress admin page).
1. $page_title (string)
The text to be displayed in the title tags of the page when the menu is selected.
2. $menu_title (string)
The text to be used for the menu.
3. $capability (string)
The capability required for this menu to be displayed to the user.
4. $menu_slug (string)
The slug name to refer to this menu by (should be unique for this menu).
5. $function — Optional. (string) => ''
The function to be called to output the content for this page.

Usage

  1. if ( !function_exists( 'add_submenu_page' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // The slug name for the parent menu (or the file name of a standard WordPress admin page). 
  5. $parent_slug = ''; 
  6.  
  7. // The text to be displayed in the title tags of the page when the menu is selected. 
  8. $page_title = ''; 
  9.  
  10. // The text to be used for the menu. 
  11. $menu_title = ''; 
  12.  
  13. // The capability required for this menu to be displayed to the user. 
  14. $capability = ''; 
  15.  
  16. // The slug name to refer to this menu by (should be unique for this menu). 
  17. $menu_slug = ''; 
  18.  
  19. // The function to be called to output the content for this page. 
  20. $function = ''; 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function); 
  24.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/plugin.php  
  1. function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ) { 
  2. global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,  
  3. $_registered_pages, $_parent_pages; 
  4.  
  5. $menu_slug = plugin_basename( $menu_slug ); 
  6. $parent_slug = plugin_basename( $parent_slug); 
  7.  
  8. if ( isset( $_wp_real_parent_file[$parent_slug] ) ) 
  9. $parent_slug = $_wp_real_parent_file[$parent_slug]; 
  10.  
  11. if ( !current_user_can( $capability ) ) { 
  12. $_wp_submenu_nopriv[$parent_slug][$menu_slug] = true; 
  13. return false; 
  14.  
  15. /** 
  16. * If the parent doesn't already have a submenu, add a link to the parent 
  17. * as the first item in the submenu. If the submenu file is the same as the 
  18. * parent file someone is trying to link back to the parent manually. In 
  19. * this case, don't automatically add a link back to avoid duplication. 
  20. */ 
  21. if (!isset( $submenu[$parent_slug] ) && $menu_slug != $parent_slug ) { 
  22. foreach ( (array)$menu as $parent_menu ) { 
  23. if ( $parent_menu[2] == $parent_slug && current_user_can( $parent_menu[1] ) ) 
  24. $submenu[$parent_slug][] = array_slice( $parent_menu, 0, 4 ); 
  25.  
  26. $submenu[$parent_slug][] = array ( $menu_title, $capability, $menu_slug, $page_title ); 
  27.  
  28. $hookname = get_plugin_page_hookname( $menu_slug, $parent_slug); 
  29. if (!empty ( $function ) && !empty ( $hookname )) 
  30. add_action( $hookname, $function ); 
  31.  
  32. $_registered_pages[$hookname] = true; 
  33.  
  34. /** 
  35. * Backward-compatibility for plugins using add_management page. 
  36. * See wp-admin/admin.php for redirect from edit.php to tools.php 
  37. */ 
  38. if ( 'tools.php' == $parent_slug ) 
  39. $_registered_pages[get_plugin_page_hookname( $menu_slug, 'edit.php')] = true; 
  40.  
  41. // No parent as top level. 
  42. $_parent_pages[$menu_slug] = $parent_slug; 
  43.  
  44. return $hookname;