Make WordPress Core

Opened 8 years ago

Last modified 6 years ago

#37920 new defect (bug)

`the_title` filter called in 2 places inconsistently

Reported by: joelworsham's profile joelworsham Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Menus Keywords: needs-patch 2nd-opinion
Focuses: Cc:

Description

This is a follow-up to #35317.

When calling wp_nav_menu() to build a frontend nav menu, the filter the_title ends up being called 2 times on page links, due to the above ticket.

It is called here https://github.com/WordPress/WordPress/blob/master/wp-includes/nav-menu.php#L754

and then here https://github.com/WordPress/WordPress/blob/master/wp-includes/class-walker-nav-menu.php#L169.

So it is called once when getting the post object title and then again when actually outputting the nav menu.

I think this is negative for 2 reasons:

1) Anyone who wants to filter the_title for nav menu items will end up having it called 2 different times, which is odd and can provide inconsistency
2) In both cases, an object ID is included, but the first call the object ID is the Post that the menu item links to (where the title comes from) and the second call the object ID is the nav menu item ID. This means, if a developer anticipates the 2 calls, they cannot check against the supplied object ID to confirm operations only happen once.

Change History (3)

#1 @swissspidy
8 years ago

  • Version trunk deleted

#2 @welcher
8 years ago

  • Keywords needs-patch added

Thanks for the ticket and welcome!

At first glance, I'm not sure if this isn't expected behaviour. I can see a use case where you may want to only run the filter on one of the two calls. For example, to only affect the nav_menu item title.

In both cases, the ID parameter belongs to an item in the wp_posts table so doing a get_post_type( $ID ) call would allow developers to apply any filter to one or both.

All that being said, did you want to write up a patch?

#3 @desrosj
6 years ago

  • Keywords 2nd-opinion added
Note: See TracTickets for help on using tickets.