Advanced caching

The Templ Cache has an option for automatic purging of cache, found in the Templ Cache plugin settings in WP Admin, which is all that is needed in most cases. See How caching works for the basics.

If that's not enough for you however, here are some examples of more advanced ways of purging Templ Cache.

How to purge Templ Cache using PHP

In case you need to purge the cache using PHP code, here's how one can do that:

$templio_cache = new templioCache();
$templio_cache->purge_zone_once();

Automatic purging using action hooks

When automatic purging of the cache has been enabled in the Templ Cache settings, the cache is automatically purged when certain actions are being run.

If you want the cache to be purged by additional actions, here's an example on how to add the action "my_action":

add_filter('templio_cache_purge_actions', function($purge_actions) {
    $purge_actions[] = 'my_action';
    return $purge_actions;
});

Purge cache over SSH

Sometimes it can be useful to purge the cache over SSH. Here's how to do that:

ssh -p <PORT> <USER>@<IPADDR> /usr/bin/templ-ccli cache purge

See SSH & WP-CLI for more on the Templ CLI and running it remotely.

Purge cache over HTTP

In other cases, it can be useful to create a "webhook" to let the cache be flushed by a HTTP request.

To do that, create a plugin file named templ_cache_purger.php with the following content, replacing your-secret-token with a long random string of your own:

<?php
/**
 * Plugin Name: Templ Cache Purger
 * Description: A tiny plugin that allows for purging of cache when a certain URL is called.
 * Version: 1.1
 */
function templ_cache_purge() {
    if(!class_exists('templioCache')) {
        return;
    }
    $templio_cache = new templioCache();
    $templio_cache->purge_zone_once();
}
add_action('plugins_loaded', function() {
    if(!isset($_GET['templ_cache_purge'])) {
        return;
    }
    if(!hash_equals('your-secret-token', (string) $_GET['templ_cache_purge'])) {
        return;
    }
    templ_cache_purge();
    die('Cache purged.');
});

The cache can then be purged by sending a request to https://example.com/?templ_cache_purge=your-secret-token. A successful request responds with Cache purged., so the caller can tell it worked.