Skip to content

Instantly share code, notes, and snippets.

@embolden
Created June 1, 2016 19:28
Show Gist options
  • Save embolden/1a739b9ab2a184368b997f87c0f42da1 to your computer and use it in GitHub Desktop.
Save embolden/1a739b9ab2a184368b997f87c0f42da1 to your computer and use it in GitHub Desktop.
<?php
function _s_nav_menu( $menu_options ) {
$transient_name = $menu_options['theme_location'] . '-menu-cache';
$menu = null;
$echo = isset( $menu_options['echo'] ) ? $menu_options['echo'] : true;
if( $menu = get_transient( $transient_name ) ) {
// Do nothing; jut assign the transient to $menu
}else {
// Reset the echo value; we need to return to set
$menu_options['echo'] = 0;
$menu = wp_nav_menu( $menu_options );
set_transient( $transient_name, $menu, DAY_IN_SECONDS );
}
if( $echo ) {
echo $menu;
}else {
return $menu;
}
}
function _s_clear_nav_menu_transient( $menu_id ) {
$theme_locations = get_nav_menu_locations();
if( $keys = array_keys( $theme_locations, $menu_id ) ) {
foreach( $keys as $key ) {
$transient_name = $key . '-menu-cache';
delete_transient( $transient_name );
}
}
}
add_action( 'wp_update_nav_menu', '_s_clear_nav_menu_transient' );
@embolden
Copy link
Author

embolden commented Jun 2, 2016

Leaving this here to remember it later: https://core.trac.wordpress.org/ticket/36225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment