Skip to main content
1 of 3
fischi
  • 7.6k
  • 2
  • 29
  • 44

Caching the WordPress Menu also gives you a performance boost. Especially if you have a lot of Pages or a giant Menu Structure, this should be considered.

Do it in 2 easy steps. At first, create a function that gets or creates the menu, instead of calling wp_nav_menu directly.

function get_cached_menu( $menuargs ) {

    $transient = 'menu_' . $menuargs['menu_id'] . '_transient';

    if ( !get_transient( $transient ) ) { // check if the menu is already cached
    
        ob_start(); // do not directly output the menu
        wp_nav_menu( $menuargs ); // build the menu with the given $menuargs
        $this_menu = ob_get_contents(); // get the HTML-code for the menu
        ob_end_clean();
        echo $this_menu; // output the menu for this run
        set_transient( $transient, $this_menu ); // set the transient, where the build HTML is saved
    
    } else {
    
        echo get_transient( $transient ); // just output the cached version
    
    }
    
}

In your theme, replace the wp_nav_menus with get_cached_menu. Now, everytime the menu is called, you have one Databasequery instead of the whole Menubuilding.

Menus don't change often - but you also have to hook into the wp_update_nav_menu action to delete the old transients.

Do it like this:

add_action('wp_update_nav_menu', 'my_delete_menu_transients');

function my_delete_menu_transients($nav_menu_selected_id) {

    $transient = 'menu_' . $nav_menu_selected_id . '_transient';

    delete_transient( $transient ); 

}

The Menu will be generated the next time the page is called - and use the cached version until someone updates the menu again.

fischi
  • 7.6k
  • 2
  • 29
  • 44