To include a custom submenu item in the WordPress admin dashboard, you can use the add_submenu_page() function. Follow these instructions:
- Open your theme’s
functions.phpfile or create a plugin file. - 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.
