Hello! I am sorry, but your website is probably slow. Your site visitors would be happier with faster page loads. Let me show you a little-used technique to speed up your WordPress website, with or without page caching. Let’s have a look… Commonly, servers spend a lot of time executing PHP code and fetching MySQL queries before they can serve a finished HTML page to site visitors. The server gets overwhelmed and your visitors get tired of waiting. Thankfully, several plugin solutions let your server do all that hard work once, then save the finished HTML page for serving to visitors later. This eases processing load off your server and pleases your visitors with faster page loads. We call these page caching solutions. Most caching solutions build entire page for serving to your visitors. Sometimes, you cannot serve cached pages. This is common on membership sites, sites with rapidly updated content, and for pages displaying personalized sections for logged-in users. Even when the entire page should not be cached, there are usually certain parts of the page that rarely change and are still ripe for the benefits of caching. In this lesson, I will show you how to cache the global menu of your website — an element that rarely changes and is often found on every page of the site. Rather than reinventing the wheel, we will use built-in WordPress functions for data blocks we call “transients.” Let’s get to work! For this demonstration I am using the “Twenty Ten” WordPress theme. Like many WordPress themes, the global navigation for this theme is found in the “header.php” file. With the header file open we need to search for a function that builds our menu for display. That function is called “wp_nav_menu.” Here we found the function in our header file. This function results in a number of PHP calls and MySQL queries. It then returns a block of HTML code that draws our navigation menu. Let’s tuck the “wp_nav_menu” function call away while we see how to work with transients. First we decide what to call this transient data. The name should be unique and descriptive. We will use “global_nav_html” for ours. Second, we check the database for a valid transient of this name. The “get_transient” function takes our transient name as a parameter. It returns all data stored in the transient if it exists, or returns FALSE if the transient does not exist or has expired. If our call to “get_transient” returned false, we need to build a fresh copy of our data and store it as a transient. We will create the data and store it in the local variable named “$local_copy.” For now, we use simple placeholder text. Next, we use the “set_transient” function to save our new transient data. The “set_transient” function needs 3 parameters. First is the name we chose for this transient, “global_menu_html.” Second is the data we want to store. In this case, we use whatever is stored in the variable named “$local_copy.” Third is the number of seconds we want this transient to remain valid. In our case, let’s set that to 5 hours,or ( 3600 * 5 ). Once we step outside the conditional phrase where we may have built a new transient, we know our local variable “$local_copy” has the data we want to use — whether it came from the database, or was created fresh. For the global menu, we simply echo that data — since it is HTML we want sent to the browser. We need to revisit placeholder text used when building fresh data. Now that we understand the order of working with transients, let’s set that data to the real HTML generated for our global navigation menu. Here is the new code for setting the “$local_copy” to the global navigation menu HTML. But, this will not work just yet. The “wp_nav_menu” function simply displays its output by default. We need the function to return the output so we can store it in our variable. To do this, we add one additional parameter to the “wp_nav_menu” array. We add “echo” and set it to “0.” There you have it: your global navigation menu will now be served quickly from the cached transient in your WordPress database. Now you see how easy it is to help speed up your WordPress website. I hope you use this on your site soon. Stay calm, and keep on truckin’.