简体   繁体   中英

Disable WP Super Cache When a Cookie is Set

I have a WordPress site which has a login managed by a 3rd party service. When a user is logged in by this service a cookie is set.

When a user is logged in (cookie set), I don't want the page to cached using WP Super Cache. I know I can use define('DONOTCACHEPAGE', TRUE); but how do I set this early enough (preferably in functions.php) when the 3rd party cookie is set?

I have tried an 'init' hook, but this doesn't get called on a cached page (I assume this is just loaded from the .html directly).

function disable_super_cache() {
  die("Cookie Logic");
  //define(‘DONOTCACHEPAGE’, TRUE);
}

add_action('init', 'disable_super_cache', 9999);

How do I set this early enough?

UPDATE

I realised I was looking at this from the wrong angle. I don't need to set the constant as an action, as it just needs to get set as early as possible. The closest I have working at the moment is the following in wp-config.php (as Super Cache is called before even root functions.php logic). But this seems like a massive workaround, better suggestions would be welcome.

/**
 * Disable WP Super Cache when a user is logged in 
 */

if( isset( $_COOKIE['LoggedInCookie'] ) ){
    $_GET['donotcachepage'] = '{secret}';
    //define( 'DONOTCACHEPAGE', 1 ) // Doesn't stop caching even in wp-config.php, bug in WP-Super-Cache??;
}

OK after a while it doesn't seem that I am going to get an answer here, so I thought it would be best to post as close as I have been able to get. I have 2 solutions, one answers my question directly but feels a bit hacky and the other is a more valid workaround.

Ideally, there should be a constant/variable that stops the caching, even if the page has already been cached. 'DONOTCACHEPAGE' stops the page being cached only if it wasn't cached already.

SOLUTION 1 (from the question)

This manually forces the $_GET['donotcachepage'] variable. Though this seems a bit hacky to me, it works.

/**
 * Disable WP Super Cache when a user is logged in 
 */

if( isset( $_COOKIE['LoggedInCookie'] ) ){
    $_GET['donotcachepage'] = '{secret}';
    //define( 'DONOTCACHEPAGE', 1 ) // Doesn't stop caching even in wp-config.php, bug in WP-Super-Cache??;
}

SOLUTION 2

Within the WP Super Cache advanced settings, there is a setting called 'Don't cache pages for known users. (Recommended)'. It basically stops caching when the user is logged into WordPress.

When a user logs in using WordPress a cookie is set called 'wordpress_logged_in_{hash}'. As my app doesn't log the user into WordPress we can create the same effect by setting a cookie with a similar name, in my case I set 'wordpress_logged_in_nocache'. This fools WP Super Cache into thinking the user is logged in so it no longer caches the page with the above setting enabled.

The second solution doesn't really answer my question directly, but if I change the cookie to the above I get the same outcome. Solution 2 feels a bit more stable to me. So I would recommend that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM