Skip to content

Implement Automated Varnish Cache Purge for Posts, Pages, CPTs & Sitemaps#11

Open
aayris wants to merge 1 commit intocloudpanel-io:masterfrom
aayrisglobal:purge-cache
Open

Implement Automated Varnish Cache Purge for Posts, Pages, CPTs & Sitemaps#11
aayris wants to merge 1 commit intocloudpanel-io:masterfrom
aayrisglobal:purge-cache

Conversation

@aayris
Copy link

@aayris aayris commented Nov 28, 2025

Summary

This pull request introduces automatic Varnish cache purging whenever WordPress content is updated.
The feature ensures that edited or newly published posts, pages, and public custom post types immediately trigger Varnish to purge the related URL(s).

To maintain indexing accuracy, this PR also purges WordPress, Yoast, and Rank-Math style sitemap URLs.

Key Features

🔄 Automatic cache purge on publish/update for:

  • Posts
  • Pages
  • Public Custom Post Types

🗺️ Automatic sitemap purge, including:

  • /wp-sitemap.xml
  • /sitemap_index.xml
  • /post-sitemap.xml
  • /page-sitemap.xml

🔐 Only purges when Varnish cache is enabled in plugin settings
📝 Ignores autosaves and revisions
🎯 Triggers only for public post types
🎉 Displays a success notice in the WordPress admin after purge
🧩 All logic added inside a dedicated class: class.varnish-cache-purge.php
🧼 Minimal changes to the main plugin loader
🔄 Fully backward-compatible

Implementation Details

A new file was introduced:

class.varnish-cache-purge.php

This class:

  • Hooks into save_post
  • Detects when purging should occur
  • Uses existing ClpVarnishCacheManager::purge_url()
  • Purges relevant sitemap URLs
  • Adds a redirect query parameter to show success messages in the admin UI

The main plugin file (clp-varnish-cache.php) was updated to load and initialize this class.
All logic follows WordPress coding standards and CloudPanel’s plugin structure.

Testing Performed

Verified auto-purge is triggered correctly under CloudPanel + Varnish
Confirmed sitemap purge execution
Tested on:

  • Posts
  • Pages
  • CPTs

Verified skip conditions:

  • Autosaves
  • Revisions
  • Draft/unpublished posts

Confirmed no errors in admin settings page
Confirmed purges only occur when the plugin’s Enable Varnish Cache option is enabled

Why This Improvement Matters

  • Prevents stale content from being served through Varnish
  • Ensures fresh content is immediately available
  • Improves SEO accuracy by keeping sitemaps current
  • Reduces manual purging and operational overhead
  • Matches modern Varnish + WordPress best practices

Backward Compatibility

This update introduces no breaking changes:

  • Admin UI remains unchanged
  • Settings structure is unchanged
  • Cache manager behavior remains intact
  • Existing manual purge functionality is unaffected

The feature is additive, safe, and optional (depends on “Enable Varnish Cache” setting).

@Ninetheme
Copy link

When WooCommerce and Elementor work together, they remember old files like post-131.css used on the homepage when they change. This needs to be triggered when Elementor generates new CSS, which is a significant issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants