0
0
WordpressHow-ToBeginner · 4 min read

How to Add Submenu Page in WordPress Admin Quickly

Use the add_submenu_page function inside an admin hook like admin_menu to add a submenu page under an existing menu in WordPress admin. Provide the parent slug, page title, menu title, capability, menu slug, and callback function to display the submenu content.
📐

Syntax

The add_submenu_page function adds a submenu page under an existing admin menu. It requires these parameters:

  • parent_slug: The slug of the parent menu to attach the submenu to.
  • page_title: The title shown on the submenu page.
  • menu_title: The text shown in the admin menu.
  • capability: The user capability required to see this submenu.
  • menu_slug: A unique slug for the submenu page.
  • callback: A function that outputs the submenu page content.
php
add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $callback );
💻

Example

This example adds a submenu page under the "Settings" menu in WordPress admin. The submenu page shows a simple message.

php
<?php
add_action('admin_menu', function() {
    add_submenu_page(
        'options-general.php', // Parent slug for Settings menu
        'Custom Submenu Page', // Page title
        'Custom Submenu',      // Menu title
        'manage_options',      // Capability
        'custom-submenu-slug', // Menu slug
        function() {           // Callback function
            echo '<h1>Welcome to the Custom Submenu Page</h1>';
            echo '<p>This is content inside your submenu page.</p>';
        }
    );
});
Output
In WordPress admin under Settings menu, a new submenu item "Custom Submenu" appears. Clicking it shows a page with heading and paragraph.
⚠️

Common Pitfalls

Common mistakes when adding submenu pages include:

  • Using an incorrect parent_slug that does not match any existing menu, so submenu won't appear.
  • Not hooking add_submenu_page inside admin_menu action, causing the submenu not to register.
  • Using a capability that the current user does not have, hiding the submenu.
  • Forgetting to provide a valid callback function, resulting in a blank page.
php
<?php
// Wrong: calling add_submenu_page outside admin_menu hook
add_submenu_page('tools.php', 'Wrong Example', 'Wrong Example', 'manage_options', 'wrong-example', function() {
    echo 'This will not show correctly';
});

// Right: inside admin_menu hook
add_action('admin_menu', function() {
    add_submenu_page('tools.php', 'Right Example', 'Right Example', 'manage_options', 'right-example', function() {
        echo 'This shows correctly';
    });
});
📊

Quick Reference

Remember these tips when adding submenu pages:

  • Always use add_submenu_page inside admin_menu hook.
  • Use correct parent_slug like options-general.php for Settings or edit.php for Posts.
  • Set capability to control who can see the submenu.
  • Provide a unique menu_slug to avoid conflicts.
  • Define a callback function to output the page content.

Key Takeaways

Use add_submenu_page inside the admin_menu hook to add submenu pages.
Provide correct parent_slug to attach submenu under the right menu.
Set capability to control access to the submenu page.
Always supply a callback function to display submenu content.
Test submenu visibility with different user roles to ensure access.