Skip to content

Instantly share code, notes, and snippets.

@the-sufi
Last active April 6, 2020 01:22
Show Gist options
  • Save the-sufi/6e2445be8c72d6b45a5b9c24e5b285e8 to your computer and use it in GitHub Desktop.
Save the-sufi/6e2445be8c72d6b45a5b9c24e5b285e8 to your computer and use it in GitHub Desktop.
WordPress - Weekly Flush all caches of W3 Total Cache Automatically
<?php
/** Flush all caches of W3TC
* Weekly basis
*/
//add weekly shcedule to wp cron
function dctit_cron_add_weekly( $schedules ) {
//adds once weekly to the existing schedules.
$schedules['weekly'] = array(
'interval' => 604800,
'display' => __( 'Once Weekly' )
);
return $schedules;
}
add_filter( 'cron_schedules', 'dctit_cron_add_weekly' );
//flush W3TC Cache - all of them
function dctit_flush_w3tc_cache() {
if ( function_exists('w3tc_flush_all') ) {
w3tc_flush_all();
}
}
add_action( 'dctit_flush_w3tc_cache_hook', 'dctit_flush_w3tc_cache');
//schedule cron event - weekly
function dctit_flush_cache_event() {
if ( ! wp_next_scheduled( 'dctit_flush_w3tc_cache_hook' ) ) {
wp_schedule_event( time(), 'weekly', 'dctit_flush_w3tc_cache_hook' );
}
}
add_action( 'wp', 'dctit_flush_cache_event' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment