Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok. I'm really stumped on this one.

Basically, I need to call a function for the Wordpress plugin W3 Total Cache as part of a cron job in crontab. I'd like to automatically clear the entire page cache nightly.

Here's the code that works fine within wordpress that I need to call:

if (function_exists('w3tc_pgcache_flush')) {

I'm currently using the following script:



define('DOING_AJAX', true);
define('WP_USE_THEMES', false);
$_SERVER = array(
    "HTTP_HOST" => "",
    "SERVER_NAME" => "",
    "REQUEST_URI" => "/",

wp_mail('', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');

if (function_exists('w3tc_pgcache_flush')) {


and the command line:

php -q /path-to-file/flushtest.php

I used the wp_mail function to test and make sure I'm getting something.

The script is working fine except that the page cache is never flushed. I get the email and there aren't any errors in the log either.

Any ideas?

Thanks for your help.

share|improve this question

closed as off-topic by gmazzap, Mark Kaplun, Pieter Goosen, ialocin, birgire Aug 25 '14 at 16:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – gmazzap, Mark Kaplun, Pieter Goosen, ialocin, birgire
If this question can be reworded to fit the rules in the help center, please edit the question.

Was my answer helpful here? – Brian Fegter Feb 17 '12 at 16:14

1 Answer 1

Here's how I would go about doing this:

First create a file with a hash for the file name within your theme directory - this one is md5('foobar'):


Within that file would be something like this:

//Use the file name as an API key of sorts
$file = explode('.', basename(__FILE__));
$key = $file[0];

//Trigger the WP function - corresponds to the foo_w3tc_flush_cron() function
define('FOO_W3TC_FLUSH', true);
define('FOO_W3TC_FLUSH_KEY', $key);

//Set headers
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');

//Set W3TC Constants
define('DONOTMINIFY', true);
define('DONOTCACHEDB', true);
define('DONOTCACHEPAGE', true);

//Set WP Constants
define('DOING_AJAX', true);

//Load WP

Add a function to your functions.php file similar to the following:

$api_key_plaintext = 'foobar';
define('FOO_W3TC_FLUSH_API_KEY', md5($api_key_plaintext));

add_action('plugins_loaded', 'foo_w3tc_flush_cron', 1);
function foo_w3tc_flush_cron(){
    $update = FOO_W3TC_FLUSH_UPDATE;

    if($update && $key == FOO_W3TC_FLUSH_API_KEY){
        if (function_exists('w3tc_pgcache_flush')){
                wp_mail('', 'Success!', 'Hello, this is an automatically scheduled email from WordPress.');
                wp_mail('', 'Failure', 'Hello, this is an automatically scheduled email from WordPress.');
        echo "Foo W3TC Cache Message"; //For logger
        die(); //We don't need the rest of WP to load


Finally, I would add the following crontab to run at 11:59pm (your server time) and include a file path to your home directory to log results:

59 23 * * * curl --header 'Cache-Control: max-age=0' >> /home/myuser/w3tc-flush.log

Hope this helps out!

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.