WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 14 months ago

#32918 new defect (bug)

_wp_menu_item_classes_by_context does not generate "current-menu-ancestor" class when the menu structure was changed by e.g. a plugin

Reported by: anaid Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.2.2
Component: Menus Keywords: has-patch needs-testing
Focuses: Cc:

Description

I am the developer of Category Posts in Custom Menu. My plugin extends Walker_Nav_Menu_Edit in order to change the menu structure. At the end of the edit loop, my plugin calls _wp_menu_item_classes_by_context on the resulting new menu structure.

In a particular case, this results in a missing "current-menu-ancestor" class, because _wp_menu_item_classes_by_context retrieves the ancestors from the database (i.e. the unchanged menu structure) instead of from the $menu_items array that was passed in and may now reflect a different structure.

Steps to reproduce with my plugin:

  • Appearances > Menus
  • Add to the menu a Page
  • Add to the menu, as child of Page, a Category that has posts
  • Check "Create submenu containing links to posts in this category."

This will result in my plugin editing the menu structure, and adding all posts to the menu instead of a link to the Category. Now navigate to one of these posts. You will see that the Page does not receive the "current-menu-ancestor" class.

I have performed a quick change in nav-menu-template.php that I have only tested with my plugin so far, so it will obviously need a rewrite. I'm attaching it nonetheless because it will quickly show in a few lines of code what took me a few paragraphs to explain here... :)

Attachments (1)

nav-menu-template.php.patch (1.8 KB) - added by anaid 2 years ago.
Patch to retrieve ancestors from the $menu_items array instead of from the database

Download all attachments as: .zip

Change History (4)

@anaid
2 years ago

Patch to retrieve ancestors from the $menu_items array instead of from the database

#1 @anaid
2 years ago

In the steps to reproduce, a last step is needed: Set "Remove original menu item" to "Never", to obtain a three-layered menu. (Otherwise, only 'current-menu-parent' would be applicable)

#2 @johnbillion
2 years ago

  • Keywords has-patch needs-testing added

Thanks for the patch, anaid! I'll review it shortly.

Related: #32367

#3 @mdgl
14 months ago

Updated patch added under #34839 where the unnecessary repetition of the code to handle menu item parent/ancestor has also been eliminated.

Note: See TracTickets for help on using tickets.