Admin Menu – Highlight top-level menu when on a sub-menu page (without showing sub-menu)

This is the current situation:

  • I am writing a plugin that has a top-level menu page
  • The plugin also contains other sub-menu pages
  • The sub-menu pages are not assigned a parent slug (thus they do not appear in the admin menu)

My requirement is to do the following:

Read More
  • Have only the top-level menu page menu item in the admin menu (no sub-menu page menu items should appear)
  • Highlight the top-level menu page menu item when on a sub-menu page

I’ve tried giving a parent slug to the sub-menu pages. This, of course, makes it appear as expected under the top-level menu item. I then tried to remove the sub-menu item, by using remove_submenu_page. However, since that removes the entire sub-menu page, it doesn’t solve the problem.

I believe the logical thing to do, is to give a parent slug, since the top-level menu item will need to know that the page being accessed falls under it. The problem then, is not showing the sub-menu page’s menu item. I am unable to find a function that deals directly with the menu items.


Is this the right approach or is there a better alternative?
Is there a action/filter/hook I can utilise to carry out what I need to do?

Edit #1:
Did some further research/testing. Setting the menu_title parameter to null or ” in the add_submenu_page call makes the title not appear. However, the <li> that contains the sub-menu item still does exist and can be interacted with. When there are numerous sub-menu pages (as in my case), the empty <li> elements will add-up and create a long blank area on the menu. This is far from ideal and as such, I’m still searching for a solution.

Edit #2:
Tried manipulating the $submenu global after the comment from @s_ha_dum. However, any removal of the menu section pertaining to a page also results in removing the page similar to remove_submenu_page and thus rendering that page inaccessible.

Related posts

1 comment

  1. That’s a bit of a late answer and I don’t know if @Jay ever sorted it out, but to anyone having the same issue, here’s how I fixed it.

    Menu Pages

    function my_admin_menu() {
    
        add_menu_page(
            'Page title',
            'Menu title',
            'manage_options',
            'my_page',
            null,
            null,
            99
        ); 
    
        add_submenu_page(
            'my_page',
            'Subpage 1 title',
            'Subpage 1 menu title',
            'manage_options',
            'my_subpage_1',
            null
        );
    
        add_submenu_page(
            'my_page',
            'Subpage 2 title',
            'Subpage 2 menu title',
            'manage_options',
            'my_subpage_2',
            null
        );
    }
    add_action( 'admin_menu', 'my_admin_menu' ) );
    
    function my_admin_head() {
    
        remove_submenu_page( 'my_page', 'my_subpage_1' );
    }
    add_action( 'admin_head', 'my_admin_head' );
    

    See the Codex for remove_submenu_page.

Comments are closed.