Automatically Flush & Purge W3TC (W3 Total Cache)

»»»Automatically Flush & Purge W3TC (W3 Total Cache)

W3 Total Cache (W3TC) is one of the most popular web performance optimization and caching plugin for WordPress. When configured, W3TC can cache dynamically generated PHP web pages as static HTML web pages, significantly increases website response time and decreases server resource usage.

In addition, W3 Total Cache also minifies and caches JavaScript (JS) and Cascading Style Sheet (CSS), caches database queries and many more other objects in order to fully optimize and speed up the WordPress website.

However, WordPress is a dynamic website after all, with many ever changing content. So there may be a need to constantly and regularly purge the caches in W3TC in specific interval automatically.

It’s possible to purge and empty selected or all caches of W3 Total Cache from within WordPress admin backend GUI. But if you need to do it at regular interval, it may be more efficient to use a WordPress built-in cron job and W3TC built-in API functions to purge and empty the caches.

To schedule a recurring WordPress cron job to automatically purge W3 Total Cache, add the following code into active theme’s functions.php file:

// Flush W3TC Cache
function tj_flush_w3tc_cache() {
	$w3_plugin_totalcache->flush_all();
}
 
// Schedule Cron Job Event 
function tj_flush_cache_event() {
	if ( ! wp_next_scheduled( 'tj_flush_cache_event' ) ) {
		wp_schedule_event( current_time( 'timestamp' ), 'daily', 'tj_flush_w3tc_cache' );
	}
} 
add_action( 'wp', 'tj_flush_cache_event' );
Note
To remove the WordPress scheduled cron job, just in case you no longer want to purge the cache, remove or comment out the above code, and add the following code to active theme’s functions.php:

function tj_disable_auto_flush_cache() {
	wp_clear_scheduled_hook('tj_flush_cache_event');
}
add_action('wp', 'tj_disable_auto_flush_cache');

The above scheduled event uses flush_all to purge and empty all W3TC caches. If you prefer, you can also selectively flush only certain types of caches. The cache flushing functions supported by W3 Total Cache are:

flush_pgcache(); // empty page cache
flush_dbcache(); // empty database cache
flush_minify(); // empty inify cache
flush_all(); // empty all caches

For example, $w3_plugin_totalcache->flush_pgcache(); purges all static HTML page caches, useful if your website requires to update some dynamic content regularly.

By | 2017-08-18T02:01:23+00:00 August 18th, 2017|Categories: Web Publishing|Tags: , , |0 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years.Connect with LK through Tech Journey on Facebook, Twitter or Google+.

Pin It on Pinterest

Share This

Share This

Share this post with your friends!