Skip to content

Instantly share code, notes, and snippets.

@JvanderHeide
Created January 20, 2014 14:23
Show Gist options
  • Save JvanderHeide/8520707 to your computer and use it in GitHub Desktop.
Save JvanderHeide/8520707 to your computer and use it in GitHub Desktop.
Place this file somewhere and call it directly or via a cron tab. It allows you to flush all, or an array of pages. I'm using this to clear the cache of a pages that contain a date reference, at midnight. This is particularly useful when using "Disk Enhanced" page caching with the W3TC plugin, since WP_cron does not work with it. You might want …
<?php
// Flush everything or just some pages
$flush_all_pages = false;
// An array of urls to flush (required if $flush_all_pages is false)
$urls = array(
'http://www.example.com/',
);
//Set W3TC Constants
define('DONOTCACHEPAGE', true);
define('DONOTCACHEDB', true);
define('DONOTMINIFY', true);
define('DONOTCDN', true);
define('DONOTCACHCEOBJECT', true);
//Set WP Constants
define('DOING_AJAX', true);
define('WP_USE_THEMES', false);
//Set headers
header('Content-type: application/json');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// Create a new empty output array
$output = array();
// Create a new empty array to output url outcomes
$urloutput = array();
// Add the list of urls to the output
if( $flush_all_pages ){
$output['requested_urls'] = 'all';
} else {
$output['requested_urls'] = $urls;
}
// Check if WordPress is available
if( file_exists($_SERVER['DOCUMENT_ROOT'].'/wp-load.php') ) {
// If it is, require it
require_once( $_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );
// Tell us that WordPress is avaiable
$output['wordpress_exists'] = true;
// Check if W3 Total Cache's page cache flush (by url) is available
if( function_exists('w3tc_pgcache_flush_url') && function_exists('w3tc_pgcache_flush') ) {
// Tell us that the w3tc function exists
$output['w3tc_exists'] = true;
//Flush the entire cache cache or the provided pages
if( $flush_all_pages ) {
$result = w3tc_pgcache_flush();
$output['w3tc_feedback'] = $result;
} else {
foreach ($urls as $url) {
$result = w3tc_pgcache_flush_url($url);
$urloutput[$url] = $result;
}
$output['w3tc_feedback'] = $urloutput;
}
} else {
// WordPress exists but we have no w3tc stuff
$output['w3tc_exists'] = false;
$output['w3tc_feedback'] = false;
}
} else {
// No WordPress, no nothing
$output['wordpress_exists'] = false;
$output['w3tc_exists'] = false;
$output['w3tc_feedback'] = false;
}
// Add a timestamp
$output['timestamp'] = date('c');
$json = json_encode($output);
// Send the array json_encoded back to the browser (optional)
echo $json;
// Finally send an e-mail to mail the results (optional)
$mail_body = $json;
$headers[] = 'Content-type: text/html';
wp_mail('mail@example.com', 'Example.com Cache Flush', $mail_body, $headers);
@mwordpress
Copy link

nice job
this what im looking for

@artipixel
Copy link

Thank you, it worked perfectly.
If you want to add it through cPanel or similar,
just go to the cron job section, create new cron job to your liking with curl command, i.e.
/usr/bin/curl https://yourwebsite.com/YOURFILE.php

@StSaens
Copy link

StSaens commented Jan 27, 2021

Very useful.
Used it to create a script that automatically flushes (and then visits) a lists of products, so they're always warm.

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