My need is to have a .php
file that is located in the root folders of all WordPress websites I manage, which can flush all cache of all sites.
Therefore, I created a file "flush-cache.php" in the root folder of the first website, and I added the following code:
<?php
/**
* Flushing the W3TC Plugin's Cache entirely, and specifically, also page cache (as it does not seem to be part of flush-all)
*
* @package WordPress
*/
ignore_user_abort( true );
/* Don't make the request block till we finish, if possible. */
if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
fastcgi_finish_request();
}
if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
die();
}
// Flush W3TC Cache right now
function flush_w3tc_cache() {
$w3_plugin_totalcache->flush_all();
}
add_action( 'wp', 'flush_w3tc_cache' );
function flush_w3tc_page() {
$w3_plugin_totalcache->flush_pgcache();
}
add_action( 'wp', 'flush_w3tc_page' );
// END Flush W3TC Cache right now
I found most of it here:
However, when I open the file /flush-cache.php, it does not seem to work. I don't get an error message or any other output (expected), but in the backend of that site, I still see that the page cache needs to be cleared.
Any suggestions what could be wrong?
My need is to have a .php
file that is located in the root folders of all WordPress websites I manage, which can flush all cache of all sites.
Therefore, I created a file "flush-cache.php" in the root folder of the first website, and I added the following code:
<?php
/**
* Flushing the W3TC Plugin's Cache entirely, and specifically, also page cache (as it does not seem to be part of flush-all)
*
* @package WordPress
*/
ignore_user_abort( true );
/* Don't make the request block till we finish, if possible. */
if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
fastcgi_finish_request();
}
if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
die();
}
// Flush W3TC Cache right now
function flush_w3tc_cache() {
$w3_plugin_totalcache->flush_all();
}
add_action( 'wp', 'flush_w3tc_cache' );
function flush_w3tc_page() {
$w3_plugin_totalcache->flush_pgcache();
}
add_action( 'wp', 'flush_w3tc_page' );
// END Flush W3TC Cache right now
I found most of it here: https://stackoverflow/questions/34509492/w3-total-cache-clear-page-cache-every-hour-automatic
However, when I open the file https://domain.xyz/flush-cache.php, it does not seem to work. I don't get an error message or any other output (expected), but in the backend of that site, I still see that the page cache needs to be cleared.
Any suggestions what could be wrong?
Share Improve this question asked Jul 15, 2019 at 9:22 codingforworlddominationcodingforworlddomination 1111 bronze badge 7 | Show 2 more comments2 Answers
Reset to default 1The working solution is:
<?php
/*
* Flushing the W3TC Plugin's Cache entirely
* @package WordPress
*/
ignore_user_abort( true );
include('/home/clients/<client-directory>/<website-directory>' . '/wp-load.php');
w3tc_flush_all();
I ended up creating a tool that can flush multiple W3TC websites simultaneously using one password-protected control panel. It’s available on Github, if this could help anyone: https://github/web-butler/w3tc-remote-flush-multiple
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745249938a4618616.html
<?php $w3_plugin_totalcache->flush_all(); ?>
to a new php file, but there I get an error "Fatal error: Uncaught Error: Call to a member function flush_all() on null in ... <file name>". – codingforworlddomination Commented Jul 15, 2019 at 11:44! empty( $_POST )
which I think means this code will only work if you post a form to it, not just open the page in your browser. Trace: you can just addecho "in the die() block";
to your script or e.g.print_r($_POST);
to dump out variables, and you'll see this output when you load the page. Your new file: yes, you'll have torequire('wp-load.php');
orrequire_once( dirname( __FILE__ ) . '/wp-load.php' );
before you can use WordPress functions. I'm not sure if that's enough to load all the plugins or if you'll have to e.g. hook 'wp' as your original code does. – Rup Commented Jul 15, 2019 at 11:50echo "<p>" . "hello" . "</p>";
before the if statement to see if there's any output and there is not, (2) addedprint_r($_POST);
but there is also no output, (3) addedecho $_POST;
inside the if before the die() but there is no output, (4) addedrequire('wp-load.php');
before the function calls, but there is no output and the result is unchanged (the page cache is still not flushed). Should I add an answer with the entire code? It seems strange to me that there is no output at all when opening the php file. – codingforworlddomination Commented Jul 15, 2019 at 14:49! empty( $_POST ) ||
from the code, as I don't need it, it will just stop the code in case there's no form posted to it. – codingforworlddomination Commented Jul 15, 2019 at 14:51