2

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"?

1

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".

3

There's not really a good way to do this. Checkout the _wp_menu_output function. It's where all the work gets done for the menu output. See that $submenu_as_parent parameter and how it's used? Setting that to false produces the result you want, but there's no filters to actually make that work. There is an open ticket on the matter, but no action has really been taken for over a year. Short of editting a core file (bad idea), you're not going to solve this without some output buffering and a very fragile solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.