Launch W3 Shared Cache Function Using Crontab

Ok I am really at a standstill on this.

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

Here is the code that works fine in Wordpress, which I need to call:

if (function_exists('w3tc_pgcache_flush')) { w3tc_pgcache_flush(); } 

I am currently using the following script:

 #!/usr/bin/php <?php define('DOING_AJAX', true); define('WP_USE_THEMES', false); $_SERVER = array( "HTTP_HOST" => "http://example.com", "SERVER_NAME" => "http://example.com", "REQUEST_URI" => "/", "REQUEST_METHOD" => "GET" ); require_once('/path-to-file/wp-load.php'); wp_mail(' email@example.com ', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.'); if (function_exists('w3tc_pgcache_flush')) { w3tc_pgcache_flush(); } ?> 

and command line:

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

I used the wp_mail function to check and make sure that I am receiving something.

The script works fine, except that the page cache is never flushed. I receive an email and there are no errors in the log.

Any ideas?

Thank you for your help.

+4
source share
2 answers

The best version will now use wp-cli . The latest version (0.9.2.8) is compatible with this plugin. Just run this command from anywhere in your wordpress directory:

 wp w3-total-cache flush 
+4
source

Change the order a little if it still works:

 w3tc_pgcache_flush(); # let it crash here so that you won't get the mail wp_mail(' email@example.com ', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.'); 
0
source

Source: https://habr.com/ru/post/1388775/


All Articles