WordPress: Cache Busting Login & Membership Issues

Website caching is critical to website design & development. It makes websites extremely fast, which improves SEO scores and makes users happy.   While there are tons of things going on behind the scenes when you visit a webpage in many cases the same things happen all the time. In the same way that you can instantly know that 2×2 is four because you memorized, we use that as a very simple way of looking at how caching works. Basically instead of always doing the heavy lifting (not that 2 x 2 is four is heavy lifting) behind the scenes the server simply remembers what the end result should be and shows that to the website visitor.

The goal of this article is not to talk about caching instead, talk about the issue of caching. Caching works great with information that seldom changes. Caching is also smart enough to know when pages change to deliver new content to the user. However, with more complex web development we occasionally run into issues with caching.  In some circumstances, membership and e-commerce websites, especially in the account management and checkout areas we can run into issues when there is heavy caching on a server.  At Four Eyes we use a hosting provider called Site Ground. They are an extremely reliable, affordable, robust, and customer-centric hosting company. They offer a wonderful set of tools for server-side cashing that greatly improve website speed and performance. It is so good that sometimes it works too well and users do not see what they expect to see.

Over the years we have run into issues where login pages, account management pages, checkout pages, or conditional content based upon user preference have not worked correctly because the server cached a page is not processing the queries and code behind the scenes specific to that user.  We do not want to disable caching on the website in total so instead, we add this little snippet of code to the theme’s function file.

//Fix Caching Issues
add_action( 'template_redirect', 'update_header_cache' );
function update_header_cache() {
if ( is_user_logged_in() ) {
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: Thu, 01 Dec 1990 16:00:00 GMT');
}
}

It is a pretty straightforward piece of code. We are simply saying if a user is logged in don’t cache anything. Granted, this is the most simplistic approach and you can easily add on to this code to do many other cool things.  It is a good starting point for making sure you’re logged in users are seeing exactly what they’re supposed to.  We have used this on many websites and it has been implemented most robustly on the online learning yoga anatomy portal Yoganatomy.