Add Submenu to WordPress Admin Panel Easily

To include a custom submenu item in the WordPress admin dashboard, you can use the add_submenu_page() function. Follow these instructions:

  1. Open your theme’s functions.php file or create a plugin file.
  2. Insert the following code snippet into the file:
// Add a top-level menu item
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');

// Add a submenu under the top-level 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 menu page content
function custom_menu_callback() {
    echo '<div class="wrap">';
    echo '<h1>Custom Menu</h1>';
    echo '<p>This is your custom menu page.</p>';
    echo '</div>';
}

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

Save your changes and reload your admin panel. A new menu titled “Custom Menu” will appear, with a submenu named “Submenu” beneath it.

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.