Make WordPress Core

Opened 11 years ago

Last modified 5 years ago

#26935 new enhancement

Add empty submenu page to hide menu page title in list

Reported by: josh401's profile josh401 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Menus Keywords: needs-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

My question has to do with add_menu_page() and add_submenu_page(). If we add a menu page, and one sub_menu page.. we end up with the main menu page being duplicated in the sub pages list.

If we have "Main" as our main page, and "Sub" as our sub page...

add_menu_page('Main', 'Main', 'manage_options', 'main_options', array($this, 'main_do_page'));
add_submenu_page('main_options', 'Sub', 'Sub', 'manage_options', 'sub_options', null);

This will create a menu structure such as:

  • Main
    • Main
    • Sub

The "Main" page is duplicated in the sub list.

Now, we can hide that duplication with a slight adjustment to the code:

add_menu_page('Main', 'Main', 'manage_options', 'main_options', array($this, 'main_do_page'));
add_submenu_page('main_options', 'Sub', 'Sub', 'manage_options', 'main_options', null);

By changing the fifth argument to match the "Main" page slug.. it will successfully hide the submenu; resulting in:

  • Main
    • Sub

However... the issue with this... is a menu item still gets created in the generated html. The inner html is blank.. so no title is actually displayed.. but the outer html is still there.

It appears in the submenu html output as such (with the second snippet above):

<li class="wp-first-item current">
    <a class="wp-first-item current" href="admin.php?page=main_options"></a>
</li>
<li>
    <a href="admin.php?page=sub_options">Options</a>
</li>

See how the top item has the html for a list item.. but has no inner html?

This creates a space, about 4 or 5 pixels.. in between the main item and the sub menu items.

Now, I know this is trivial when there is only one menu item. But, now that I know it exists.. it is driving me nuts.

  1. It results in unnecessary html output.
  2. It results in a space in the menu item list.. which is faint, but unattractive... and hinders the visual appeal of the new admin panel.

Could we perhaps first check if the add_submenu_page() fifth argument ($menu_slug) matches the add_menu_page() first argument ($page_title)... then the outer html wrapper doesn't get rendered?

With space example(http://joshlobe.com/testsite/images/trac1)
Without space example(http://joshlobe.com/testsite/images/trac2)

Thank you for reading.

Change History (3)

#1 @SergeyBiryukov
11 years ago

  • Description modified (diff)
  • Version changed from trunk to 3.8

Related: #19085

#2 @wonderboymusic
9 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

#3 @josh401
9 years ago

Updated image links (sorry about that):

With space example: http://joshlobe.com/images/trac1.PNG
Without space example: http://joshlobe.com/images/trac2.PNG

Version 0, edited 9 years ago by josh401 (next)
Note: See TracTickets for help on using tickets.