actions - W3TC manual cache flush does not work

My need is to have a .php file that is located in the root folders of all WordPress websites I manage, which can flush a

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
  • 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 Commented Jul 15, 2019 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/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>". – codingforworlddomination Commented Jul 15, 2019 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 Commented Jul 15, 2019 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. – codingforworlddomination Commented Jul 15, 2019 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. – codingforworlddomination Commented Jul 15, 2019 at 14:51
 |  Show 2 more comments

2 Answers 2

Reset to default 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();

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

相关推荐

  • actions - W3TC manual cache flush does not work

    My need is to have a .php file that is located in the root folders of all WordPress websites I manage, which can flush a

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信