Does loading of sub pages in menu cause load to the server?

I am working on a wp site where I need to manage 500s of child pages for a single page such as About menu can have more child pages need to manage those child pages as sub menu and I have more than 5 such menus.

My client is saying that it will cause load to the server. Does this create a problem in server? If yes, is there any way to load the menu that will not cause any problem to the server.

Read More

Thanks in advance…

Related posts

Leave a Reply

2 comments

  1. In general it should not be a big problem. The built of the menu could be slow, so your best option here is to cache the menus with transient.

    if ( !get_transient( 'first_menu_transient' ) ) {
    
        ob_start(); // do not directly output the menu
    
        // build the menu
        $first_menu = ob_get_contents();
        ob_end_clean();
        echo $first_menu;
        set_transient( 'first_menu_transient', $first_menu );
    
    } else {
    
        echo get_transient( 'first_menu_transient' );
    
    }
    

    In this way you reduce the databasequeries to a minimum, compared to building the whole menu each time.

    To avoid having the wrong menu after changing and saving the menu, delete the transient on the wp_update_nav_menu action.

    add_action('wp_update_nav_menu', 'my_delete_menu_transients');
    
    function my_delete_menu_transients($nav_menu_selected_id) {
    
        delete_transient( 'first_menu_transient' ); // you should also just delete the transient of the updated menu, but you get my point - you would have to write the function for linking the menu-IDs to your transient names. For example, just put the ID of the menu in the transient name.
    
    }
    

    Everything clear so far?

  2. Yes, it will create high load for the basic WordPress installation. To be able to have a functioning site you will have to employ caching. If the site is static in nature then using caching plugins like super-cache or w3tc can be enough, but if you can’t use any of them because the site is very dynamic you will have to cache the menu in your code and do something like

    In your theme’s functions.php file add

    add_action('save_post','regenrate_menu_cache'); // regenerate the cache when a new page might have been added
    add_action('delete_post','regenrate_menu_cache'); // regenerate the cache when a page might have been deleted
    
    function regenrate_menu_cache() {
      $menu = wp_nav_menu(array("echo" => false,"menu" => "my_menu")); // get the HTML that is generated for the menu
      update_option('my_menu_cache',$menu); // and store it in the DB as an option
    }
    

    in your theme’s header.php file replace the menu generation code with

    echo get_option('my_menu_cache');
    

    The drawback with this code is that each time a post/page/attachment etc is saved you recalculate the menu which might make saving slower, so you might want the recalculation to be limited to only when a page on menu is changed.

    Side note: With so many pages in hierarchy the pages admin might become slow.