Function and usage of add_submenu_page() in wordpress


Answers ( 1 )


    The add_submenu_page() function in WordPress is used to add a submenu page to an existing top-level menu in the WordPress dashboard. Here's a detailed breakdown of the function and its usage:


    • Purpose: Adds a submenu page to the WordPress dashboard.


    • This function is used to create a new submenu under an existing menu in the WordPress admin area.
    • It checks the user's capabilities to determine if the submenu should be displayed to them.
    • The function responsible for rendering the output of the page must also verify that the user has the necessary capabilities.


    1. $parent_slug (string, required):
      • The slug name for the parent menu. This can either be the slug of a top-level menu page or the file name of a standard WordPress admin page.
    2. $page_title (string, required):
      • The text displayed in the title tags of the page when the menu is selected.
    3. $menu_title (string, required):
      • The text used for the menu.
    4. $capability (string, required):
      • The capability required for this menu to be displayed to the user. It determines who can see this menu item, based on user roles and capabilities.
    5. $menu_slug (string, required):
      • The slug name to refer to this menu. It should be unique for this menu and follow the formatting rules compatible with sanitize_key().
    6. $callback (callable, optional):
      • The function called to output the content for this page. Default is an empty string.
    7. $position (int|float, optional):
      • The position in the menu order where this item should appear. Default is null.


    • Returns a string which is the resulting page’s hook_suffix, or false if the user does not have the required capability.

    Sample Usage:

    Here's an example of how to use add_submenu_page():

    function my_custom_submenu_page() {
            'edit.php',               // $parent_slug
            'My Custom Submenu',      // $page_title
            'Custom Submenu',         // $menu_title
            'manage_options',         // $capability
            'my-custom-submenu',      // $menu_slug
            'my_custom_submenu_page_callback'  // $callback
    add_action('admin_menu', 'my_custom_submenu_page');
    function my_custom_submenu_page_callback() {
        // Code to display content for the submenu page
        echo '<h1>Welcome to My Custom Submenu</h1>';

    In this example, a submenu is added under the 'Edit' menu in the WordPress admin. The submenu is titled "Custom Submenu", and it is only accessible to users with the 'manage_options' capability. The content of the submenu page is defined in the my_custom_submenu_page_callback function.

Leave an answer