add_submenu_page() wordpress function and usage This text is not used because the data attribute has a value

add_submenu_page() wordpress function and usage

Home / WordPress / WordPress Wiki / add_submenu_page() wordpress function and usage


add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position = null )
Add a submenu page.


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.



(string) (Required) The slug name for the parent menu (or the file name of a standard WordPress admin page).


(string) (Required) The text to be displayed in the title tags of the page when the menu is selected.


(string) (Required) The text to be used for the menu.


(string) (Required) The capability required for this menu to be displayed to the user.


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


(callable) (Optional) The function to be called to output the content for this page.

Default value: ”


(int) (Optional) The position in the menu order this item should appear.

Default value: null


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

More Information


  • 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:
    • $parent_slugget_admin_page_parent()
    • $page_titleget_admin_page_title(), or simply global $title
    • $menu_slugglobal $plugin_page.
  • 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

    Sample Usage

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’

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

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'.


 * Adds a submenu page under a custom post type parent.
function books_register_ref_page() {
        __( 'Books Shortcode Reference', 'textdomain' ),
        __( 'Shortcode Reference', 'textdomain' ),
 * 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>

Example submenu with php class

 * Sub menu class
 * @author Mostafa <>
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() {
            '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();


add_action('admin_menu', 'wpdocs_register_my_custom_submenu_page');
function wpdocs_register_my_custom_submenu_page() {
        'My Custom Submenu Page',
        'My Custom Submenu Page',
        'wpdocs_my_custom_submenu_page_callback' );

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() {
        null,   //or 'options.php'
        'My Custom Submenu Page',
        'My Custom Submenu Page',

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

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.


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


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


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments