Reputation: 41
I have setup cloudflare to cache everything for my wordpress site. My page rules are like the following:
mydomain.tld/wp-admin Cache Level - Bypass
mydomain.tld/*?*loggedin=true Cache Level - Bypass
mydomain.tld/* Cache Level - Cache Everything
To avoid caching admin-bar, i only enable admin-bar-rendering in wordpress when url has parameter "loggedin=true".
It's working great but I wonder if there is a way to make it work without having the extra "loggedin=true" parameter?
This post is closest to my situation. Currently the solutions proposed from the discussion are either:
Upvotes: 3
Views: 1368
Reputation: 56
Rather than using a URL parameter, you can use a cookie. This is much easier since adding the cookie is automatically done for you by WordPress, and creating the rule in Cloudflare is really simple (and available on the free tier).
When logged in, WordPress sets a cookie called wordpress_logged_in_[hash] that contains some data about the user account. You can create a Cloudflare Cache Rule that looks for this and bypasses the cache.
Let's create a Cache Rule for our Zone that will match on the cookie. Start by going to the following link and selecting your Zone (it is a deep link, so it should take you to where you need to create the rule): https://dash.cloudflare.com/?to=/:account/:zone/caching/cache-rules/new
WordPress Logged In Bypass
wordpress_logged_in_*=*
Notice the wildcard for the hash in the key and the wildcard for the value. If you only wanted to bypass cache for a single user, you could do something like wordpress_logged_in_*=johndoe|*
where johndoe
is the user's WordPress username.You can test the rule manually on the site, but I would suggest testing using Cloudflare Trace. Trace will let you easily forge the cookie and shows you exactly which rules are applied.
https://example.com
)wordpress_logged_in_hashGoesHere
johndoe|1234567890|randomchars|randomchars
After a few moments, you should see a "Matched" result under "Cache Rules" like the one below:
Upvotes: 0