How to Add a Submenu in WordPress Dashboard

To add a submenu under a custom menu in the WordPress admin panel, you can use the built-in add_submenu_page() function. Here’s how:

  1. Open functions.php from your theme directory, or build a plugin.
  2. Paste the following code into the file:
// Top-level admin menu
function custom_menu_page() {
    add_menu_page(
        'Custom Menu',
        'Custom Menu',
        'manage_options',
        'custom-menu',
        'custom_menu_callback',
        'dashicons-admin-generic',
        25
    );
}
add_action('admin_menu', 'custom_menu_page');

// Submenu below custom menu
function custom_submenu_page() {
    add_submenu_page(
        'custom-menu',
        'Submenu',
        'Submenu',
        'manage_options',
        'custom-submenu',
        'custom_submenu_callback'
    );
}
add_action('admin_menu', 'custom_submenu_page');

// Main page callback
function custom_menu_callback() {
    echo '<div class="wrap">';
    echo '<h1>Custom Menu</h1>';
    echo '<p>Welcome to your custom admin menu page.</p>';
    echo '</div>';
}

// Submenu page callback
function custom_submenu_callback() {
    echo '<div class="wrap">';
    echo '<h1>Submenu</h1>';
    echo '<p>This is your submenu section.</p>';
    echo '</div>';
}

Once added, you’ll see the “Custom Menu” with its “Submenu” below it inside the admin dashboard. Customize content as needed.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.