How to change the behaviour of cache purge plugin?

Last Updated -


How to change the the behaviour pagely-cache-purge.php mu plugin?


By default, the Pagely Managment Plugin installed on all WP sites actively listens for WordPress hooks when a save_post/etc are triggered from the /wp-admin/ section, in which it does subsequent PURGE requests on the servers caching layer itself to clear the sites homepge/pages/feeds cached content. Which in practice would mean that visitors are always getting the latest news articles when they're updated.

But excessive cache purging on too many pages can degrade server performance. After getting the PURGE request, the purge worker on the server has to then scan the disk on the filesystem to see if it has any matches for whatever page-cache exists, being a pretty expensive operation to do. Every PURGE request is essentially invalidating the cache for amount of pages (the default TTL of cached html content is ~ 30 minutes)

Disabling the cache purge can be done numerous ways. You can disable all or part of the cache PURGE requests that happen during a WP Post hook.

pagely_cache_purge_path hook can be used for changing cache purge behavior, used via add_filter. See examples below:

Disable All cache purge during WP Post save requests

Create custom plugin with a content below:

// disable cache purge (wp-content/mu-plugins/custom-stop-site-cache-purging.php)
add_filter('pagely_cache_purge_path', function($url) {

   // to disable purging "return null" from the filter - useful during batch imports, etc)
   return null; 

Disable purge for the homepage only, when only comments are made

Similar to previous example but for the homepage only:

// disable cache purge on comments from happening on homepage

add_filter('pagely_cache_purge_path', function($url) {
        if ($url == "/") {
            // Only stop homepage from purging
            return null;
        } else {
            return $url;

Disable during Cron run

// Add code to your cron at the top

Purge All Cache

if ( class_exists( 'PagelyCachePurge' ) ) {
        $purger = new PagelyCachePurge();

Purge Specific Items

if ( class_exists( 'PagelyCachePurge' ) ) {
    $purger = new PagelyCachePurge();

    // the pagely-cache-purge.php:purgePath() function is the main hook which checks & stops all subsequent PURGE requests.

Hooks for purging cache for everything/specific objects, used via apply_filters:

  • pagely_purge_all (yup, purge everything)
  • pagely_purge_comment
  • pagely_purge_post
  • pagely_purge_common (purge the front page, feeds, and contents of $PAGELY_CACHE_PURGE_ALWAYS)
  • pagely_cache_purge_after (callback function utilized after an action is taken)

You can set a variable called PAGELY_CACHE_PURGE_ALWAYS that specifies endpoints that you want to be purged any time a cache purge action takes place. This can be thrown into an MU plugin:

$PAGELY_CACHE_PURGE_ALWAYS = [ '/notifications/' ];

Lets do a full example...

Lets say we added the following php file to make sure our commands run...

$ cat test-cache-purge-hook.php

add_action('pagely_cache_purge_after', function($target) {
    error_log("Cache purge for $target");

we should have a working command that will look something like this...


Please note that the example above utilizes the parse_url function

Related Topics

Pagely is the Managed WordPress Hosting Platform designed to exceed the needs of media, business, and Enterprise customers alike. We help the world's biggest brands scale WordPress.

Copyright © 2006-2017 Pagely, Inc. All rights reserved.
Pagely® and WordPress® are registered trademarks.

Powered by Zendesk