User Agents

Web browsers and everyday web tech, explained

Cloudflare Cache Rules for WordPress: A Guide

· 5 min read · Cookies and Cache

In this article
  1. Why WordPress Needs Special Cache Rules
  2. How Cloudflare Cache Rules Work
  3. Choosing the Right Cache Rules for Your Site
  4. Common Problems and Misconceptions
  5. Cache Rules vs. Page Rules vs. WordPress Plugin
  6. Cost and Plan Considerations
  7. Safety and Best Practices
  8. Frequently Asked Questions

Cloudflare cache rules for WordPress let you control exactly which URLs, file types, or logged-in user states get cached at Cloudflare's edge, overriding the default settings. You set them in the Cloudflare dashboard under Caching → Cache Rules, and they apply before other cache settings like Page Rules or the WordPress plugin's defaults.

Why WordPress Needs Special Cache Rules

WordPress is dynamic by nature. Every page request runs PHP and queries the database, which is why caching is essential for performance. However, caching the wrong content breaks functionality: a logged-in user's admin bar, a shopping cart, or a page that changes per visitor must not be served from a shared cache. Cloudflare's default behavior already respects WordPress cookies (like wordpress_logged_in_*), but cache rules give you finer control—for example, caching the homepage for anonymous visitors while bypassing it for logged-in users, or caching static assets for longer periods.

How Cloudflare Cache Rules Work

A cache rule is a combination of an expression (when to match) and an action (what to do). You create rules in the Cloudflare dashboard under Caching → Cache Rules. The expression uses fields like URI, Hostname, Cookie, or Device Type. The action can set the Cache Level (e.g., Cache Everything or Bypass), the Edge TTL, and whether to honor origin cache headers. Rules are evaluated in order; the first matching rule wins.

For WordPress, a typical rule might be:

  • Expression: (http.host eq "example.com" and not starts_with(http.request.uri.path, "/wp-admin"))
  • Action: Cache Everything, Edge TTL 1 hour, and set Respect Origin to off.

This caches all front-end pages for an hour, but excludes the admin area. You can also add a condition to bypass for logged-in users by checking the Cookie field for wordpress_logged_in.

Choosing the Right Cache Rules for Your Site

The correct rules depend on your site's nature. Here are common scenarios:

  • Blog or content site: Cache everything except /wp-admin, /wp-login.php, and when a logged-in cookie is present. Set a TTL of 1–6 hours.
  • WooCommerce: Never cache cart, checkout, or my-account pages. Bypass when cookies like woocommerce_items_in_cart or woocommerce_cart_hash are present. Cache product pages for anonymous users, but with a short TTL (10–30 minutes) to reflect stock changes.
  • Membership sites: Bypass cache for any request with a logged-in cookie, or use Cache Everything only for anonymous visitors.
  • Static assets: Cache CSS, JS, images, and fonts for a long TTL (e.g., 30 days) because they are versioned by WordPress themes and plugins.

Always test rules on a staging site or with a small sample of traffic before applying globally.

Common Problems and Misconceptions

A frequent mistake is setting Cache Everything without excluding logged-in users. This causes a logged-in user to see a cached page meant for anonymous visitors, breaking the admin bar or showing stale content. Another issue is caching pages that contain personalized content (like “Hello, John”)—even if the page is cached, the dynamic part will be frozen for all visitors.

Misconception: “Cloudflare cache rules replace WordPress caching plugins.” In reality, they work together. Cloudflare caches at the edge, but your WordPress server still needs to generate the page once. A plugin like WP Super Cache or W3 Total Cache can speed up the origin, but Cloudflare's edge cache reduces the number of requests hitting your server.

Another problem: not purging the cache after updating content. Cloudflare's automatic purge on WordPress updates works only if you use the official Cloudflare plugin. With cache rules, you may need to manually purge the cache or use the API to purge specific URLs after publishing.

Cache Rules vs. Page Rules vs. WordPress Plugin

Cloudflare offers multiple ways to control caching, which can be confusing:

  • Page Rules: The older, simpler method. You can set “Cache Level: Cache Everything” for a URL pattern, but it lacks fine-grained conditions (like cookie checks) and is limited to 125 rules.
  • Cache Rules: More powerful, with expressions and multiple actions. They are evaluated before Page Rules, and you can have many more.
  • Cloudflare WordPress Plugin: Automatically sets recommended settings, purges cache on updates, and can be configured to bypass logged-in users. It doesn't replace cache rules; it complements them.

For most WordPress sites, using the plugin plus a few cache rules is the best balance. If you need complex logic (e.g., based on cookies or device), cache rules are the way to go.

Cost and Plan Considerations

Cache rules are available on all Cloudflare plans, but the number of rules you can create varies. Free plans allow a limited number (currently 10), while paid plans (Pro, Business, Enterprise) allow more. The exact limits are listed in Cloudflare's documentation. There is no extra cost for using cache rules beyond your plan subscription. However, if you exceed the rule limit, you must delete or consolidate rules. For most WordPress sites, 10 rules are sufficient.

Safety and Best Practices

Cache rules are safe if you follow these practices:

  • Always bypass caching for /wp-admin and /wp-login.php.
  • Use a condition to bypass for any logged-in user by checking for the wordpress_logged_in cookie.
  • Set a reasonable TTL—not too short (defeats the purpose) and not too long (stale content). For dynamic sites, 1 hour is a good default.
  • Test with a specific URL using Cloudflare's “Cache” tool in the dashboard (under Caching → Configuration) to verify the rule works.
  • If you use a caching plugin, ensure its cache headers (like Cache-Control) are respected or overridden intentionally.
  • Regularly purge the cache after major updates, or use the plugin's automatic purge.

Frequently Asked Questions

Do I need cache rules if I use a WordPress caching plugin?

Not necessarily, but they help. A plugin caches on the server, but Cloudflare's edge cache reduces latency for visitors worldwide. Cache rules let you control what Cloudflare caches, which can improve performance further. Many sites use both.

How do I exclude logged-in users from the cache?

In your cache rule expression, add a condition that checks the Cookie field for wordpress_logged_in. For example: not any(cf.edge.server_ip) and not http.cookie contains "wordpress_logged_in". Then set the action to Bypass or Cache Everything only when the cookie is absent.

What is the best TTL for WordPress pages?

It depends on how often you update content. For a blog, 1–6 hours is typical. For a news site, 10–60 minutes. For a static landing page, 24 hours or more. Balance freshness with performance.

Why is my WordPress admin area being cached?

Your cache rule might be too broad, or you haven't excluded /wp-admin. Add a condition to your expression: not starts_with(http.request.uri.path, "/wp-admin"). Also ensure you're not using a Page Rule that caches everything without exceptions.

Can I use cache rules to cache the WooCommerce cart?

No, you should never cache cart, checkout, or my-account pages because they are user-specific. Instead, create a rule that bypasses caching for those paths and for cookies like woocommerce_items_in_cart.