Speed up WordPress CMS Menu Loading

Overview

We can customize the WordPress navigation menus with writing few lines of codes.

wp_nav_menu is the WordPress menu function to display custom menus created in domain.com/wp-admin>Appearance>Menus panel . It is a must use function but it’s not perfect for all website or projects. Default menu function OK for small websites but if your site is large and have more traffic with several menus then It may create website speed and performance issues.

What should i do for performance upgrade for wp_nav_menu?

Majority site menu are not updated frequently so its a good idea to cache your menus for long expired period.

How to cache your menu?

If your WordPress version is 3.9+ you can use Transients API and pre_wp_nav_menu. I am not going to write long story here. for your convenience i added my code below.

Please copy this code and past it to you Theme function.php file

/**
* Cache wp_nav_menu to speed up your wordpress blog menus. you can customize this code as your need.
**/
add_filter( 'pre_wp_nav_menu', 'ct_get_nav_menu_cache', 10, 2 );
function ct_get_nav_menu_cache( $nav_menu, $args ) {
    $cache_key      = ct_get_nav_menu_cache_key($args);
    $cached_menu    = get_transient( $cache_key );
    if ( ! empty( $cached_menu ) )
        return $cached_menu;
 
    return $nav_menu;
}
 
add_filter( 'wp_nav_menu', 'ct_set_nav_menu_cache', 10, 2 );
function ct_set_nav_menu_cache( $nav_menu, $args ) {
    $cache_key      = ct_get_nav_menu_cache_key($args);
    set_transient( $cache_key, $nav_menu, 86400 );
 
    return $nav_menu;
}
 
function ct_get_nav_menu_cache_key($args){
    $timestamp = get_transient('nav-menu-cache-timestamp');
    if($time === false){
        $timestamp = time();
        set_transient( 'nav-menu-cache-timestamp', $time, 86400 );
    }
    return apply_filters( 'nav_menu_cache_key' , 'nav-menu-' . md5( serialize( $args ) ) . $timestamp );
}
 
// delete the cache when update the menu
add_action( 'wp_update_nav_menu', 'ct_delete_nav_menu_cache' );
function ct_delete_nav_menu_cache( $menu_id, $menu_data){
    set_transient( 'nav-menu-cache-timestamp', time(), 86400 );
}

Comments

comments

Saint Martin

Share
Published by
Saint Martin

Recent Posts

Unlocking the Best 5 Powers of Business Directories in the USA

Visibility is key in the busy world of online business. Just as a beacon guides… Read More

6 months ago

Crafting Effective Facebook Ad Copy: Best Practices for Success 2024

Facebook Ad Copy: Writing Facebook ad copy is a fundamental aspect of a successful ad… Read More

6 months ago

Conducting Effective Penetration Testing for Website Security

Introduction Penetration testing, often referred to as pen testing or ethical hacking, plays a pivotal… Read More

6 months ago

Apple’s iPhone 15 Pro and Pro Max: Unveiling the Next Era of Smartphone Brilliance

Introduction The smartphone industry is no stranger to innovation. Year after year, manufacturers strive to… Read More

7 months ago

iPhone 15 Pro and Pro Max: Astonishing Features That Will Change the Way You Use Your Phone.

In the world of modern smartphones, surprises are becoming increasingly rare. Breakthrough features and astounding… Read More

7 months ago

Mental Health disorder: The Power of Prioritizing Well-being and Resilience

Introduction: In today's fast-paced and demanding world, it is vital to recognize the significance of… Read More

10 months ago

This website uses cookies.