WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 23 months 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:

Description

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
4 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
4 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 4 years ago by ianatkins (previous) (diff)

#3 @swissspidy
3 years ago

#36798 was marked as a duplicate.

#4 @lukecavanagh
3 years ago

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

#6 @tazotodua
3 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:

<li>
  <a class="p284">
</li>

that means POST,with id 284

or

<li>
  <a class="c284">
</li>

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 3 years ago by tazotodua (previous) (next) (diff)

#7 @lukecavanagh
3 years ago

@ianatkins

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

https://github.com/inpsyde/menu-cache

#8 @szepe.viktor
2 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.
https://github.com/szepeviktor/tiny-cache/blob/master/tiny-nav-menu-cache.php

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 2 years ago by szepe.viktor (previous) (diff)

#9 @tazotodua
23 months ago

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

Note: See TracTickets for help on using tickets.