WP Manifestindependent plugin directory
manifest / performance / geekswipe-cache-purge

Geekswipe Cache Purge

Purges Cloudflare's cache when something visitors can see changes. Spam and comments waiting for moderation never purge.

★ 0stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/geekswipe/geekswipe-cache-purge/archive/refs/heads/main.zip
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

Cloudflare can cache your WordPress pages in data centres around the world, so a reader in Sydney gets the page from Sydney instead of waiting on your server in London. The catch is that Cloudflare has no idea when a page changes. Fix a typo or approve a comment, and readers keep getting the old copy until Cloudflare's expires.

This plugin tells Cloudflare. When something a visitor can see changes, it asks Cloudflare to purge the site's cache, and the next visitor to each page gets a fresh copy from your server. If you run W3 Total Cache, it empties W3TC's page cache first, so Cloudflare doesn't pick up the stale copy from there instead.

It's one PHP file with no settings page, and it runs on Geekswipe. It was written with AI, and AI.md says how and what I've checked.

What purges the cache?

  • A post, page, bbPress forum, topic, or reply that's published, updated while published, unpublished, closed, moved, trashed, marked as spam, or deleted.
  • A comment that's approved, or one that was approved and is then edited, unapproved, trashed, marked as spam, or deleted. A comment that's approved the moment it arrives counts too, like your own reply from the dashboard.
  • A category, tag, or topic tag that's renamed or deleted.
  • Saving a menu, switching themes, or saving the Customizer.
  • Updating an active plugin, or the active theme or its parent. A theme update usually changes its CSS, and pages Cloudflare already holds would keep pointing at the old version.
  • W3TC's Purge All Caches button, and the Purge button in its page cache settings.

A request purges once, however many things changed in it. So approving fifty comments in one bulk action is one purge.

What doesn't?

Spam. A blog with open comments can catch dozens of spam comments a day, and purging on each of them would keep emptying Cloudflare's cache for nothing. Comments waiting for moderation don't purge either, and neither do drafts, pending posts, autosaves, or revisions. A scheduled post purges when it goes live. W3TC's own scheduled purges stay away from Cloudflare too.

Why not W3TC's Cloudflare extension?

W3 Total Cache has a Cloudflare extension of its own, and it purges Cloudflare whenever W3TC flushes anything. W3TC flushes a post's page every time a comment arrives, without checking whether it's spam. So the extension purges your whole zone for every spam comment as well.

Why purge the whole site?

A change rarely stays on one page. A new comment changes its post, the comment count on the home page and in the category archive, and maybe a recent comments list on every page. Tracking every page a change touches means knowing all of your theme's lists and widgets, and missing one leaves a stale page. Purging everything is one API call. The cost is that each page comes from your server once more after a change.

That suits a site that changes a few times a day at most. Cloudflare's free plan allows 5 purges a minute per account, and a site that changes every few minutes would spend most of its time with an empty cache anyway.

What you need

  • WordPress 6.4 or later
  • PHP 8.1 or later
  • A site on Cloudflare
  • W3 Total Cache, if you use it

Cloudflare doesn't cache HTML unless a Cache Rule tells it to, so without one there's nothing to purge. A rule for WordPress should cache GET requests that have no query string. It should skip /wp-admin/, wp-*.php, /wp-json/, and xmlrpc.php, and skip anyone carrying a wordpress_logged_in_, wordpress_sec_, comment_author_, or wp-postpass_ cookie. Then members never get someone else's page, and commenters see their own comment while it waits for moderation.

If you use Akismet, keep Cloudflare's lifetime for pages, plus your page cache's, under about 10 hours. Akismet puts a nonce in every comment form and tells its servers whether the nonce on a new comment was still valid. A WordPress nonce stops being valid 12 to 24 hours after the page was made.

Installation

  1. Download geekswipe-cache-purge.zip from the latest release.

  2. In WordPress, go to Plugins › Add New Plugin › Upload Plugin and upload the zip.

  3. In Cloudflare, go to My Profile › API Tokens and create a custom token with one permission, Zone › Cache Purge › Purge, limited to your site's zone. You can also limit it to your server's IP addresses under Client IP Address Filtering, so a leaked token is useless anywhere else.

  4. Add the token and the zone's ID to wp-config.php. The zone ID is on the zone's Overview page in Cloudflare.

    define( 'GEEKSWIPE_CACHE_PURGE_ZONE_ID', 'your zone ID' );
    define( 'GEEKSWIPE_CACHE_PURGE_TOKEN', 'your API token' );
  5. Activate the plugin.

Without both constants the plugin sends nothing to Cloudflare, and PHP's error log says what it would have purged. That makes it safe to run on a local copy of your site.

How do I know it worked?

Every purge writes a line to PHP's error log, with what changed.

Geekswipe Cache Purge: purged Cloudflare. Changed: post 1234 updated
Geekswipe Cache Purge: purged Cloudflare. Changed: comment 5678 unapproved to approved on post 1234

If Cloudflare refuses a purge or doesn't answer, the error goes to the same log, and administrators see a notice in the dashboard until a purge works. Pages Cloudflare already holds stay stale until they expire, so check the token and the zone ID first.

Limitations

  • It purges the whole zone. If other sites share the zone, their cache goes too.
  • Changing widgets, or settings outside the Customizer, doesn't purge. Use W3TC's Purge All Caches, or Purge Everything in Cloudflare's dashboard.
  • W3TC's Purge Current Page button only empties W3TC's copy of that page.
  • Multisite hasn't been tested.

Building a release zip

git archive --format=zip --prefix=geekswipe-cache-purge/ -o geekswipe-cache-purge.zip HEAD

Licence

GPL-2.0-or-later. See LICENSE.