WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 2 weeks ago

#32918 closed defect (bug) (wontfix)

_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: 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 4 years ago.
Patch to retrieve ancestors from the $menu_items array instead of from the database

Download all attachments as: .zip

Change History (5)

@anaid
4 years ago

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

#1 @anaid
4 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
4 years ago

  • Keywords has-patch needs-testing added

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

Related: #32367

#3 @mdgl
2 years ago

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

#4 @iseulde
2 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

This ticket has not seen any activity in over *two* years, so I'm closing it as "wontfix".

The ticket may lack decisiveness, may have become irrelevant, or may not have gathered enough interest.

If you think this ticket does deserve some attention again, feel free to reopen.

For bugs, it would be great if you could provide updated steps to reproduce against the latest version of WordPress (5.0.2 at the time of writing). Remember images or a video can be superior to explain a problem. At the very least, quickly test again to make sure the bug still exists.

If it’s an enhancement or feature, some extra motivation may help.

Thank you for your contributions to WordPress! <3

Note: See TracTickets for help on using tickets.