Caching in WordPress: Caching and cache busting
Hi, my name is Tina! I am a Full Stack Software Developer and Tech Lead from Canada. 🙋🏻♀️
Search for a command to run...
Hi, my name is Tina! I am a Full Stack Software Developer and Tech Lead from Canada. 🙋🏻♀️
No comments yet. Be the first to comment.
Have you ever installed a package by using go install <some-package> and wondered how to uninstall it? Go is really great in that respect. Go packages are a single binary and they’re installed based on how your Go tooling is set up. Provided we have ...

Overview This is the 3rd article in a series of posts about working with Open API to create generated API clients and documentation websites. In the 1st post we went over what we can do with Open API spec files, e.g. generate API clients and documen...

Overview This article is the 2nd in a series of blog posts that discuss using Open API to help with API documentation and API client code generation. In the previous article, we went over how we can generate documentation and API clients using existi...

Overview This article starts a series of Open API-related blog posts. OpenAPI, previously known as Swagger (a name still in use today for many of its tools), is a suite of tools that generate documentation and networking clients for various programmi...

You've probably already seen the scary alert (that's possibly why you're here), but in case you haven't, it looks something like this: NewAppIJustDownloaded pasted from your clipboard Sometimes it h

There are plenty of options for developers and clients in terms of caching in WordPress. You may have heard of popular caching plugins like W3 Total Cache, CDNs like Cloud Flare, or managed hosting services like WP Engine that offer caching features, all of which promise to greatly increase page performance. In this post I'll go over why caching in WordPress is important, the different kinds of cache available to you, and how to bust the cache so that your visitors don't get outdated cached stylesheet and JavaScript assets.
There are plenty of reasons to use caching in WordPress.
One of the reasons to implement caching in WordPress is that caching functionality is built into WordPress by default. WordPress automatically appends a version query parameter to the stylesheet URI, which is a nice setup for developers to implement specific criteria for which to cache files. See more in the Different types of caching available section.
A common problem with many low-quality WordPress websites is that the site performance is terrible. This is common in all those trendy click-bait websites you end up visiting from social media networks. One of the reasons those websites are so slow is because they serve advertisements, but it is also highly likely that they have not implemented efficient caching to improve the user experience on their website. If you have a product- or service-based business—rather than one that generates revenue through advertising—you may care about the user experience on your website.
Better caching means that your server will be working less, which is important as your website scales and you get more visitors. You likely already know this, but if your server gets overloaded it means that your website could go offline. This can happen if the server-side script and/or database use too much memory and crash. A common error is one that says "Error establishing a database connection." This error occurs for a number of reasons.
Implementing server-side caching will help alleviate stress on your server resources, which would prevent your application from using memory when it isn't necessary, which could save you from having to troubleshoot down time on your website.

There are a variety of different types of caching available, as previously mentioned.
WordPress ships with a built-in caching feature out-of-the-box. This allows you to assign a version number to your assets. Your visitors' browser then stores this version with the contents of the file in its cache. When the version your browser has is different than the version your website is serving, the browser will request a fresh copy with the new version number. In the source code of your WordPress website you may have noticed a path similar to: http://mydomain.com/wp-content/themes/my-theme/style.css?ver=4.6.1. This is WordPress' built-in caching functionality. The WordPress version is appended to the version query parameter by default, but developers can override it to be more intelligent. With the default option, the cache would only be flushed when you upgrade your WordPress core install, which doesn't happen as often as it should, and could likely happen less often than you update your website. This means that your visitors may not be seeing the newest styles and scripts when you want them to. Please see the section on Cache busting below for more information on solving this problem.
There are services available called Content Delivery Networks. They provide a number of services, but one of the most valuable ones is caching. A CDN is an intermediary service that sits between your server and your visitors. Traffic goes through the CDN firstly, and then is routed to your server. It essentially acts as a highly-efficient middleman (or woman), and only goes back to your server when necessary. Common CDN providers include CloudFlare and CloudFront. These services can help protect your website against Distributed Denial of Service (DDoS) attacks, which are attacks with the sole purpose of overloading your server resources enough to make it crash. If a CDN is sitting in front of your website, the CDN gets this load, and they have intelligent measures in place to prevent the traffic from going through to your website.
When hosting your own WordPress website, you are responsible for ensuring that the server stays up, and if you have quick access to a web developer or systems administrator, then this is a great option to avoid the higher costs of managed hosting services. This has the lowest up-front cost, since you can download WordPress and a theme for free, and pay approximately $5-15 per month for web hosting or $5-$20 per month on a Virtual Private Server with root access. There are plugins available—a popular one being W3 Total Cache—which are developed to make WordPress efficient with caching. This plugin is great if you prefer to manage server-side caching at the application level rather than the infrastructure level. Setting up server-side caching using WordPress plugins on a self-hosted WordPress installation involves time and knowledge in many areas, including but not limited to software development, infrastructure, and networking.
If you would like to implement server-side caching but do not want to rely on a systems administrator or web developer to manage caching at the application or infrastructure levels, you can opt for a managed hosting service like WP Engine. Managed hosting services come at a higher cost (upwards of $30 per month) because there are teams of developers and developer operations guys and gals standing by to make sure your website never goes down. These managed hosting services can provide great value for clients whose websites experience heavy loads. Managed hosting services often have their own more intelligent forms of server-side caching implemented. These features are available as an alternative to managing caching with plugins on your self-hosted WordPress install. The benefits of going with a managed hosting service can include features such as:
Managed hosting provides great value, and, in my opinion is worth the additional cost if it is important that your website doesn't go down.
As mentioned above, WordPress comes with client-side caching out-of-the-box. Currently it appends the WordPress core version to the file. It's much more intelligent to have the version code be generated using the file modified date to output, for example, this: style.css?ver=201612050921, which is a much more accurate representation of when you would like to update the file from cache. If the file has been modified and the version your browser tries to get is different than the version stored, then the browser will get the newer version. You can read the file modified date in your functions file using PHP.
You can also ensure that your cache is busted when you deploy a new version of your site. Content Delivery Networks like CloudFlare have an API endpoint that you can hit to programmatically clear cache after a deploy. If you automate your site deployment (or deploy with one command), you can also add cache purging to the automation process. In order to clear your CDN cache programmatically, you will need to learn how to use the API of your CDN, which would likely involve creating/finding and using an API key and your website's identifier, and making an API request to the specific endpoint with instructions to clear the cache.
Depending on your setup, this could vary. If you use W3 Total Cache on a self-hosted WordPress installation, you will need to clear the cache through their interface in your WordPress dashboard.
If you used a managed hosting service like WP Engine, you can easily do that through their menu option in the WordPress dashboard. In the general settings, there is a button that says Purge All Caches. You can click that to purge the cache.

You don't always have to purge the cache on a managed hosting service, but if you encounter issues, you may want to:
We aggressively cache everything from pages to feeds to 301-redirects on sub-domains; this makes your site load lightning-fast for your non-logged-in readers. 99.9% of the time this is what you want, but every once in a while something happens where our cache should have been purged but wasn't. For example, some URL plugins change behavior without alerting WordPress. You use this button to purge all caches -- on our caching proxies, on the CDN, in memcached, everything.
Essentially, caching in WordPress is a great idea that is easy to implement. It will increase your site speed and reliability, which makes the User Experience of your website much more enjoyable. Caching using a Content Delivery Network offloads some work from your server, and client-side caching in the browser means your users only download the assets they need. Purging cache is fairly simple for a skilled developer to implement. If you have a WordPress site there really is no reason to not use caching—it'll make your life and the life of your visitors easier! Contact me if you'd like to discuss getting a WordPress website or improving your current one.