
As the title suggests, I wish to avoid the main menu from becoming a submenu.
When I generate a submenu under an admin menu, it inadvertently turns the main menu into a submenu too. How can I prevent this from happening? Additionally, when clicking on the main menu, I want it to navigate to the first submenu rather than a separate main menu page. Here is the code.
// Hook to add the custom menu
add_action('admin_menu', 'custom_admin_menu');
// Function to create the custom menu
function custom_admin_menu() {
// Add top-level menu
add_menu_page(
'Custom Menu', // Page title
'Custom Menu', // Menu title
'manage_options', // Capability
'custom-menu', // Menu slug
'custom_menu_display', // Callback function to display the menu page
'dashicons-chart-bar' // Icon URL or Dashicon class
);
// Add submenu
add_submenu_page(
'custom-menu', // Parent menu slug
'Custom Submenu', // Page title
'Custom Submenu', // Menu title
'manage_options', // Capability
'custom-submenu', // Menu slug
'custom_submenu_display' // Callback function to display the submenu page
);
// Add another submenu
add_submenu_page(
'custom-menu', // Parent menu slug
'Another Submenu', // Page title
'Another Submenu', // Menu title
'manage_options', // Capability
'another-submenu', // Menu slug
'another_submenu_display' // Callback function to display the submenu page
);
}
// Function to display the menu page
function custom_menu_display() {
?>
<div class="wrap">
<h2>Custom Menu</h2>
<p>This is my custom menu page content.</p>
</div>
<?php
}
// Function to display the custom submenu page
function custom_submenu_display() {
?>
<div class="wrap">
<h2>Custom Submenu</h2>
<p>This is my custom submenu page content.</p>
</div>
<?php
}
// Function to display the another submenu page
function another_submenu_display() {
?>
<div class="wrap">
<h2>Another Submenu</h2>
<p>This is my another submenu page content.</p>
</div>
<?php
}