I have nginx caching setup that ignores logged in users, but I have a lot of users in subscriber roles who SHOULD be served a mostly cached site. Is there a way to only ignore caching for SOME roles (like contributor and above) while leaving caching on for subscribers (and potentially other roles at some point)?
1 Answer
Here is what I ended up doing:
// Set disable cache for certain roles
add_action('init', 'add_custom_cookie_admin');
function add_custom_cookie_admin() {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$thisrole = $current_user->roles[0];
if($thisrole !== 'subscriber') {
setcookie("disable_cache", $current_user->user_login, time()+43200, COOKIEPATH, COOKIE_DOMAIN);
}
}
}
// and then remove the cookie on logout
function clear_custom_cookie_on_logout() {
unset($_COOKIE["disable_cache"]);
setcookie( "disable_cache", '', time() - ( 15 * 60 ) );
}
add_action('wp_logout', 'clear_custom_cookie_on_logout');
And then I added this to my nginx cache:
if ($http_cookie ~* "disable_cache") {
set $skip_cache 1;
}