This page is concerned with the optimization of server-side processing, including database queries. For optimization of client-side assets, please see Asset Optimization and Image Optimization
The WordPress menu system generation process is a relatively expensive operation, processing-wise.
First, WordPress needs to look up the appropriate menu, and retrieve all the menu items, along with their metadata from the database. Then it needs to sort all of those items into the correct order. For each menu item, it also needs to find the associated post or taxonomy term, and look up the correct URL to link to. Then it needs to process all of that data to produce the menu markup.
With UberMenu, all these same processes that the WordPress core system handles are handled by UberMenu. Since UberMenu provides additional features beyond what the standard menu system offers, this may require additional processing as well, depending on how you configure your menu.
Here are some examples of additional processing:
- Images – If you attach an image to a menu item, UberMenu will need to look up that image’s metadata and URL to add it to the menu
- Dynamic Items – If you use dynamic items, UberMenu will need to query the database to determine which posts or terms to display, as well as look up metadata for each of those items
- Menu Segments – Using menu segments requires an additional lookup to query that menu.
Sometimes, other plugins or themes will also run filters on the menu items, which adds additional processing as well.
Now, this is not to say that you shouldn’t use these features, or that there is anything wrong – naturally, more advanced features are going to require some additional processing to provide those features. However, you should remember to use these features efficiently and consider using caching to optimize your load time.
Each of the features has a valid use; just make sure you are using them in an appropriate way for optimal performance. The ideal usage will depend on your site and your needs.
Is this an issue at all?
Whether optimization is something you need to be concerned about depends on your site. If you have a low traffic site, or if you only have a few menu items, the additional processing probably won’t ever be noticeable.
If you have a large menu, or if you have a high traffic site, optimization becomes much more important.
With each new menu item, additional lookups and processing must occur. With each new visitor, that extra processing increases the load on your server.
Designing your menu efficiently
The first thing you should do is to make sure you design your menu efficiently. Starting out with a strong foundation is always best, even if you intend to use caching. Here are some suggestions:
Don’t use Dynamic Items for static content
Use dynamic items for their purpose: dynamic content. If you have a “latest posts” section that you want to display, it makes sense to use Dynamic Items as these items are dynamic – they will change frequently. It makes more sense to allow WordPress to do the work and look them up, rather than changing them constantly by hand.
If, on the other hand, you have a list of items that will never change, or change only very infrequently, it is more efficient to set up those items manually. While it is an extra effort up front, it’ll save your site from having to run an extra query on each page load, increasing site performance. Using dynamic items in such a case is unnecessary because the results never change.
Use images efficiently
For an overview on image size, lazy loading, and compression, please see Image Optimization
When configuring your images, you’ll notice the “Inherit Featured Image” setting can be set to “Dynamically Inherit”. This means that the image will be looked up by checking the featured image of the associated post each time this menu item is processed. If the image is not going to be changing, it is more efficient to use “Assign Image on Save”, or use “Disabled” and just assign your image manually. The Dynamically Inherit setting is useful if your featured image changes regularly, or when using Dynamic Posts.
Use Menu Segments efficiently
Menu Segments are great for organizing larger menus into more manageable pieces. However, bear in mind that there is a slight overhead for each segment, as it requires an extra query to look up each segment. The overhead is minimal, but you should keep your number of menu segments reasonable – perhaps one for each top level menu item. (Note that if you use Menu Segment Caching, there will be no extra overhead and you will actually save a lot of processing)
Caching the result to eliminate menu processing
Once you have optimized your menu design, you can further reduce your server processor load by caching the entire menu (or a section of the menu with Menu Segment Caching).
The WordPress menu system needs to run a variety of database queries and process the results with PHP to build a chunk of HTML that becomes the menu markup. By caching this result, you can display the output multiple times without re-running your entire menu process, saving you that processing.
There are three strategies for this, which I recommend implementing in this order:
1. Page Caching
If your site is high-traffic enough for you to be concerned with optimization, it would likely benefit from the use of a caching plugin.
Many managed hosting solutions (for example, WPEngine), offer their own built-in caching solution. The advantage is that they maintain and tune this cache, so you don’t need to worry about it – it just works.
If your host does not provide a caching solution, you can make use of a WordPress plugin like W3 Total Cache to do the same thing.
The caching plugin will cache the entire contents of each page, meaning that the database and PHP processing are bypassed entirely. This includes the menu.
2. Menu Segment Caching
Menu Segment caching is built into UberMenu as of version 3.2.1. By using Menu Segments for your submenus, you can easily leverage this caching strategy to save processing in your submenus. Read more about Menu Segment Caching
Menu Segment caching overlaps a lot with the third type (Menu Caching), but Menu Segment Caching’s advantage is that it gives you big wins with less side effects (less of a concern about caching dynamic items like current menu item classes), so it’s often the best solution.
3. Menu Caching
Page caching caches individual pages – but generally only for logged-out users (since logged-in users see individualized content, caching generally isn’t very useful in these instances). The page is still processed in full for logged-in users.
If you have a large number of logged-in users, you can consider caching just the menu via the WP Menu Cache plugin (currently in beta). This plugin caches the output of your menu as a transient, returning the entire output in a single database query.