how to only add a top-level admin menu without it creating a sub-level-menu

Here is a the code snippet:

    add_action( 'admin_menu', 'travel_site' );

function travel_site(){
    add_menu_page( 'Travel Site Menu', 'Travel Site', 'manage_options', 'travel-site-menu', 'ts_admin_main_page' );
    add_submenu_page("travel-site-menu","View Travel Requests","View Travel Requests","manage_options","ts-view-travel-requests","ts_admin_vtr_page");
}

function ts_admin_main_page(){
    echo '<div class="wrap">';
    echo    '<p>Testing main travel site menu page</p>';
    echo '</div>';
}

function ts_admin_vtr_page(){
    echo '<div class="wrap">';
    echo    '<p>Testing view travel requests</p>';
    echo '</div>';
}

The problem is when adding the sub menu, the name of the top level menu(“Travel Site”) creates itself as a sub-level menu. See image: enter image description here

Basically I don’t want “Travel Site” sub-menu under the main menu “Travel Site”, how do i remove the submenu “Travel Site”?

2 Answers
2

Effectively you can do this by adding a sublevel item with the same menu slug as the top level, while changing the submenu item title. This avoids the look of duplicate menu items, even though the destination of the top level, and the first sub menu item are the same.

You have to have at least two sub menu items, and one will be a duplicate destination as your top level menu item.

For example:

add_action( 'admin_menu', 'add_travel_menu' );
function add_travel_menu() {
        add_menu_page('Travel Site Menu', 'Travel Site', 'manage_options', 'travel-site-menu', 'ts_admin_main_page');
        add_submenu_page('travel-site-menu', 'Travel Site', 'View Travel Site', 'manage_options', 'travel-site-menu', 'ts_admin_main_page');
        add_submenu_page('travel-site-menu', 'View Travel Requests', 'View Travel Requests', 'manage_options', 'ts-view-travel-requests', 'ts_admin_main_page');
}
function ts_admin_main_page() {
    if ( !current_user_can( 'manage_options' ) )  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    echo '<div class="wrap">';
    echo '<p>Here is where the form would go if I actually had options.</p>';
    echo '</div>';
}

This would create the Following Menu:

  • Travel Site -> travel-site-menu
    • View Travel Site -> travel-site-menu
    • View Travel Requests -> ts-view-travel-requests

enter image description here

The function ts_admin_main_page actually creates the page that will be the destination for “Travel Site” and “View Travel Site”. You would need to create the the similar function “ts_admin_vtr_page” that will create the sub page destination for “View Travel Requests”.

Leave a Comment