add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback = , int|float $position = null ): string|false

Adds a submenu page.

Description

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.

Parameters

$parent_slugstringrequired
The slug name for the parent menu (or the file name of a standard WordPress admin page).
$page_titlestringrequired
The text to be displayed in the title tags of the page when the menu is selected.
$menu_titlestringrequired
The text to be used for the menu.
$capabilitystringrequired
The capability required for this menu to be displayed to the user.
$menu_slugstringrequired
The slug name to refer to this menu by. Should be unique for this menu and only include lowercase alphanumeric, dashes, and underscores characters to be compatible with sanitize_key() .
$callbackcallableoptional
The function to be called to output the content for this page.

Default:''

$positionint|floatoptional
The position in the menu order this item should appear.

Default:null

Return

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

More Information

Notes

  • 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.
  • For $menu_slug please don’t use __FILE__ it makes for an ugly URL, and is a minor security nuisance.
  • Within the rendering function $function you may want to access parameters you used in add_submenu_page(), such as the $page_title. Typically, these will work:
  • 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

Source

function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $callback = '', $position = null ) {
	global $submenu, $menu, $_wp_real_parent_file, $_wp_submenu_nopriv,
		$_registered_pages, $_parent_pages;

	$menu_slug   = plugin_basename( $menu_slug );
	$parent_slug = plugin_basename( $parent_slug );

	if ( isset( $_wp_real_parent_file[ $parent_slug ] ) ) {
		$parent_slug = $_wp_real_parent_file[ $parent_slug ];
	}

	if ( ! current_user_can( $capability ) ) {
		$_wp_submenu_nopriv[ $parent_slug ][ $menu_slug ] = true;
		return false;
	}

	/*
	 * If the parent doesn't already have a submenu, add a link to the parent
	 * as the first item in the submenu. If the submenu file is the same as the
	 * parent file someone is trying to link back to the parent manually. In
	 * this case, don't automatically add a link back to avoid duplication.
	 */
	if ( ! isset( $submenu[ $parent_slug ] ) && $menu_slug !== $parent_slug ) {
		foreach ( (array) $menu as $parent_menu ) {
			if ( $parent_menu[2] === $parent_slug && current_user_can( $parent_menu[1] ) ) {
				$submenu[ $parent_slug ][] = array_slice( $parent_menu, 0, 4 );
			}
		}
	}

	$new_sub_menu = array( $menu_title, $capability, $menu_slug, $page_title );

	if ( null !== $position && ! is_numeric( $position ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: %s: add_submenu_page() */
				__( 'The seventh parameter passed to %s should be numeric representing menu position.' ),
				'<code>add_submenu_page()</code>'
			),
			'5.3.0'
		);
		$position = null;
	}

	if (
		null === $position ||
		( ! isset( $submenu[ $parent_slug ] ) || $position >= count( $submenu[ $parent_slug ] ) )
	) {
		$submenu[ $parent_slug ][] = $new_sub_menu;
	} else {
		// Test for a negative position.
		$position = max( $position, 0 );
		if ( 0 === $position ) {
			// For negative or `0` positions, prepend the submenu.
			array_unshift( $submenu[ $parent_slug ], $new_sub_menu );
		} else {
			$position = absint( $position );
			// Grab all of the items before the insertion point.
			$before_items = array_slice( $submenu[ $parent_slug ], 0, $position, true );
			// Grab all of the items after the insertion point.
			$after_items = array_slice( $submenu[ $parent_slug ], $position, null, true );
			// Add the new item.
			$before_items[] = $new_sub_menu;
			// Merge the items.
			$submenu[ $parent_slug ] = array_merge( $before_items, $after_items );
		}
	}

	// Sort the parent array.
	ksort( $submenu[ $parent_slug ] );

	$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug );
	if ( ! empty( $callback ) && ! empty( $hookname ) ) {
		add_action( $hookname, $callback );
	}

	$_registered_pages[ $hookname ] = true;

	/*
	 * Backward-compatibility for plugins using add_management_page().
	 * See wp-admin/admin.php for redirect from edit.php to tools.php.
	 */
	if ( 'tools.php' === $parent_slug ) {
		$_registered_pages[ get_plugin_page_hookname( $menu_slug, 'edit.php' ) ] = true;
	}

	// No parent as top level.
	$_parent_pages[ $menu_slug ] = $parent_slug;

	return $hookname;
}

Changelog

VersionDescription
5.3.0Added the $position parameter.
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 15 content

    Slugs for $parent_slug (first parameter)

    Dashboard: ‘index.php’
    Posts: ‘edit.php’
    Media: ‘upload.php’
    Pages: ‘edit.php?post_type=page’
    Comments: ‘edit-comments.php’
    Custom Post Types: ‘edit.php?post_type=your_post_type’
    Appearance: ‘themes.php’
    Plugins: ‘plugins.php’
    Users: ‘users.php’
    Tools: ‘tools.php’
    Settings: ‘options-general.php’
    Network Settings: ‘settings.php’

  2. Skip to note 16 content

    Adding a submenu page to a custom post type
    If you want to add a submenu type to a custom post type, such as a reference page for a custom post type created by a plugin, you can use for the $parent_slug parameter whatever you see up top on the “All Posts” view for that post type. For instance, for a custom post type “Book,” the $parent_slug could be 'edit.php?post_type=book'.

    Example:

    /**
     * Adds a submenu page under a custom post type parent.
     */
    function books_register_ref_page() {
        add_submenu_page(
            'edit.php?post_type=book',
            __( 'Books Shortcode Reference', 'textdomain' ),
            __( 'Shortcode Reference', 'textdomain' ),
            'manage_options',
            'books-shortcode-ref',
            'books_ref_page_callback'
        );
    }
    
    /**
     * Display callback for the submenu page.
     */
    function books_ref_page_callback() { 
        ?>
        <div class="wrap">
            <h1><?php _e( 'Books Shortcode Reference', 'textdomain' ); ?></h1>
            <p><?php _e( 'Helpful stuff here', 'textdomain' ); ?></p>
        </div>
        <?php
    }
  3. Skip to note 17 content

    Inside menu created with add_menu_page()
    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');
  4. Skip to note 18 content

    Example submenu with php class

    /**
     * Sub menu class
     *
     * @author Mostafa <mostafa.soufi@hotmail.com>
     */
    class Sub_menu {
    
    	/**
    	 * Autoload method
    	 * @return void
    	 */
    	public function __construct() {
    		add_action( 'admin_menu', array(&$this, 'register_sub_menu') );
    	}
    
    	/**
    	 * Register submenu
    	 * @return void
    	 */
    	public function register_sub_menu() {
    		add_submenu_page( 
    			'options-general.php', 'Submenu title', 'Submenu title', 'manage_options', 'submenu-page', array(&$this, 'submenu_page_callback')
    		);
    	}
    
    	/**
    	 * Render submenu
    	 * @return void
    	 */
    	public function submenu_page_callback() {
    		echo '<div class="wrap">';
    		echo '<h2>Submenu title</h2>';
    		echo '</div>';
    	}
    
    }
    
    new Sub_menu();

  5. Skip to note 19 content

    Example

    add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
    
    function wpdocs_register_my_custom_submenu_page() {
    	add_submenu_page(
    		'tools.php',
    		'My Custom Submenu Page',
    		'My Custom Submenu Page',
    		'manage_options',
    		'my-custom-submenu-page',
    		'wpdocs_my_custom_submenu_page_callback' );
    }
    
    function wpdocs_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', 'wpdocs_register_my_custom_submenu_page');
    
    function wpdocs_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',
    	);
    }
  6. Skip to note 20 content

    Regarding the 1st note in the More Information section above, you may also run into the “Sorry, you are not allowed to access this page.” message even if you’ve hooked correctly into the admin_menu hook. This error message will happen if all of these are true:

    • Your parent page is a custom page that you added using add_menu_page, and
    • That parent page was created in another plugin, not the same plugin that is creating the submenu page, and
    • You didn’t add a lower priority to the admin_menu hook for the add_submenu_page call, so the submenu page may be fired before the parent page–causing an error.

    Solution: Add a lower priority, such as 99, to the action that creates the submenu page. For example:

    add_action( 'admin_menu', 'wpdocs_register_my_custom_submenu_page', 99 );
  7. Skip to note 21 content

    When working with the Classes, You can add_submenu_page by following Make sure the callable is static function.

    add_submenu_page( 'admin_menu', 'Custom Menu', 'My Custom Menu', 'manage_options', 'my-custom-menu', __CLASS__ .'::menu_page_output' );
    
    public menu_page_output() {
    	//Menu Page output code
    }
  8. Skip to note 22 content
    add_submenu_page( 
    		null,
    		'WP Docs Submenu Page',
    		'WP Docs Submenu Page',
    		'manage_options',
    		'wpdocs-submenu-page',
    		'wpdocs_submenu_page_callback',
    	);

    In some special cases (like above) if you pass null in parent_slug, it will generates deprecation warnings when running under PHP 8.1, because the provided $parent_slug is passed to strpos() and to str_replace(). Instead use empty string if needed like:

    add_submenu_page( 
    		'',   //or 'options.php'
    		'WP Docs Submenu Page',
    		'WP Docs Submenu Page',
    		'manage_options',
    		'wpdocs-submenu-page',
    		'wpdocs_submenu_page_callback',
    	);
  9. Skip to note 23 content

    To anyone else troubleshooting an unexpected issue with this function, please PAY ATTENTION to the final argument, specifically the $function, it has to be a STRING, the name of the function, not a call to the function itself. Amateur mistake, I know, but sometimes you just make the simplest of errors.

    Bad:

    add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug', my-output-function());

    Good:

    add_menu_page('My Custom Page', 'My Custom Page', 'manage_options', 'my-top-level-slug', 'my-output-function');
  10. Skip to note 24 content

    To further clarify adding a page without it showing in the menu/submenu, using this code:

    add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
     
    function wpdocs_register_my_custom_submenu_page() {
        add_submenu_page( 
    		'options.php',
            'My Custom Submenu Page',
            'My Custom Submenu Page',
            'manage_options',
            'my-custom-submenu-page',
            'my_custom_submenu_page_callback',
        );
    }

    You would then access this page via this URL:
    /wp-admin/options.php?page=my-custom-submenu-page

  11. Skip to note 25 content

    If you are working within a Class, the “function” parameter for the add_submenu_page should be an array, with the first value in the array being an instance of the class while the second value in the array will be a method in the object (given as a string).

    Example below

    Class WPDocs_AdminPage
    {
        private $_plugin_name;
    
        private $_version;
    
        public function __construct( $plugin_name, $version )
        {
            $this->_plugin_name = $plugin_name;
            $this->_version = $version;
        }
    
        public function wpdocs_create_menu_and_submenu_page()
        {
            add_menu_page(
                'PAGE TITLE', 'MENU TITLE', 'CAPABILITY', 'menu_slug',
                array( $this, 'wpdocs_method_name_in_the_class' ), 'icon_url', 'POSITION'
            );
            add_submenu_page(
                'parent_slug', 'PAGE TITLE', 'MENU TITLE', 'CAPABILITY', 'menu_slug', 
                array( $this, 'wpdocs_method_name_in_the_class' )
            );
        }
    }
    $admin_page = new WPDocs_AdminPage;
    add_action( 'admin_menu', array( $admin_page, 'wpdocs_create_menu_and_submenu_page' ) );
  12. Skip to note 26 content

    If you’re trying to put your custom sub page under a url like:

    https://www.example.com/wp-admin/admin.php?page=custom-settings

    Then you should just use the custom-settings for the parent slug.

    In my use case I wanted to put a custom taxonomy under an ACF Options Page, and I used this:

            add_submenu_page(
                'custom-settings',
                'Product Filter By Ambient Noise',
                'Product Filter By Ambient Noise',
                'manage_options',
                'edit-tags.php?taxonomy=product-filter-ambient-noise'
            );
  13. Skip to note 27 content

    There is something missing which it is not obvious at all:

    When a menu page has 0 or 1 submenu pages, no submenus will show up.

    A menu page contains a page by itself.

    A menu page that contains 1 submenu page, will appear as the same as if it didn’t have a submenu page. This is because the first-level menu element and the first second-level menu belonging to that first-level menu are meant to show the same page.

    In other words, you can’t have a single submenu element, because that doesn’t seem to make sense for WordPress.

    “Only one page? Just use the menu element itself then! No need for a dropdown.”

    Notice that whenever you click on a menu item found in the admin interface that has subitems, it will load its first subitem. So the menu item and its first subitem are essentially the same. However, you are able to rename the subitem.

    Some examples of this:

    – Dashboard = Home
    – Posts = All Posts
    – Appearance = Themes
    – Tools = Available Tools

    When a menu page contains 2 or more submenu pages, the submenu pages will become visible. That’s what most of us don’t know at first.

    So the trick is to create a submenu page identical to the menu page (you can change the $page_title and $menu_title if you want) and then your additional submenu page:

    add_menu_page( '', 'Menu Item', $capability, 'parent_slug', $callback );
    add_submenu_page( 'parent_slug', 'Page Title', 'First Subitem', $capability, 'parent_slug', $callback );
    add_submenu_page( 'parent_slug', 'Second Page Title', 'Second Subitem', $capability, 'submenu_slug', $submenu_callback );

    As you can see, the `$page_title` for `add_menu_page` could even be left as an empty string (but not `null`), because the title used for the page will be the first submenu’s `$page_title`. But I recommend to introduce the same string for readability.

You must log in before being able to contribute a note or feedback.