WordPress.org

Make WordPress Core

Opened 16 months ago

Last modified 14 months ago

#37920 new defect (bug)

`the_title` filter called in 2 places inconsistently

Reported by: joelworsham Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Menus Keywords: needs-patch
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 (2)

#1 @swissspidy
15 months ago

  • Version trunk deleted

#2 @welcher
14 months 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?

Note: See TracTickets for help on using tickets.