0

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.com/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?

  • You can't call WordPress functions without requiring wp-load. However you ought to get some error output for this. Are you hitting the die(), e.g. are you requesting the page with a GET? It might be worth adding some trace output. (And some security to the endpoint.) – Rup Jul 15 '19 at 10:13
  • Thanks for the response @Rup I am simply opening the php file in my browser. The code I posted is the entire code I have in the file (no die() anywhere). How do I add a trace output? And security? I'd like to mention that this solution is maybe a bit of an overkill? After reading this link (tiny.cc/m0iq9y) I tried just adding <?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>". – sunyamare Jul 15 '19 at 11:44
  • You've got ! 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 add echo "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 to require('wp-load.php'); or require_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 Jul 15 '19 at 11:50
  • I've tried the following: (1) added echo "<p>" . "hello" . "</p>"; before the if statement to see if there's any output and there is not, (2) added print_r($_POST); but there is also no output, (3) added echo $_POST; inside the if before the die() but there is no output, (4) added require('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. – sunyamare Jul 15 '19 at 14:49
  • ... and I will remove ! 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. – sunyamare Jul 15 '19 at 14:51
1

The 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();
0

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.com/web-butler/w3tc-remote-flush-multiple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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