Make WordPress Core

Opened 5 years ago

Last modified 4 years ago

#36225 new enhancement

Cache wp_nav_menu with Transient API

Reported by: ianatkins Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.4.2
Component: Menus Keywords:
Focuses: performance Cc:


Forgive me if I'm wrong but it looks like wp_nav_menu output isn't cached with the transient API. We're manually caching the output in our theme, then clearing the transient on the 'wp_update_nav_menu' hook.

For sites with caching plugins this is obviously redundant, but with dynamic pages (e.g. WooCommerce carts) this would be beneficial.

Change History (9)

#1 @ocean90
5 years ago

Related: #27120, in particular ticket:27120:9.

I don't think that we can store the whole output of that function because it would bypass any filters which could be used by plugins.

#2 @ianatkins
5 years ago

@ocean90 not a WordPress internal's pro, but could the caching not happen on line 452 of wp-includes/nav-menu-template.php - after the filter has been run?

Thanks for digging out the related tickets. Re ticket:27120:9 - whilst caching at a lower level is great, these menu's are relatively intensive to build and it's not uncommon for us to have 4-5 on a page on larger sites. Could be a nice perf gain for a lot of users.

Last edited 5 years ago by ianatkins (previous) (diff)

#3 @swissspidy
5 years ago

#36798 was marked as a duplicate.

#4 @lukecavanagh
5 years ago

I can see this as being a performance gain for WC stores with a fairly large menu built out.

#6 @tazotodua
5 years ago

better idea! please, why dont you add this to WP-core?:
attach each link ("<a>" tag) the id of the object. for example:

  <a class="p284">

that means POST,with id 284


  <a class="c284">

that means category,with id 284

and so on... It will be very good!
for this, there exists a filter:

function add_menu_atts( $atts, $item, $args ) {
	$atts['onClick'] = 'return true';    return $atts;
add_filter( 'nav_menu_link_attributes', 'add_menu_atts', 10, 3 );
Version 1, edited 5 years ago by tazotodua (previous) (next) (diff)

#7 @lukecavanagh
4 years ago


This also works very well, using it on a client site.


#8 @szepe.viktor
4 years ago

Core has a lot of caching done in object cache. See post caching for example.

My implementation also works with object cache although it will not work without one.

It has hooks for flushing the nav menu cache on menu editing, and the cache key is

md5( serialize( $args ) . $_SERVER['REQUEST_URI'] )
Last edited 4 years ago by szepe.viktor (previous) (diff)

#9 @tazotodua
4 years ago

it will be good if this topic will be ever implemented.

Note: See TracTickets for help on using tickets.