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:
- Open
functions.phpfrom your theme directory, or build a plugin. - 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.
