kovshenin / surge

Surge is a very simple and fast page caching plugin for WordPress.
GNU General Public License v3.0
152 stars 12 forks source link

Enable cache for logged in users #22

Open roryashfordbentley opened 2 years ago

roryashfordbentley commented 2 years ago

Hey, first of all thanks for the excellent plugin, it works incredibly well out of the box!

I have been poking through the plugin source code and I can't seem to find the logic that disables/prevents caching for logged in users.

What I am hoping is possible is that I can create a custom config file like this Surge Configuration Example. And setup some logic to enable caching for logged in users with a certain custom role (essentially a role that only has frontend access).

If you could point me in the direction of how Surge currently identifies logged in users I'd really appreciate it.

doiftrue commented 2 years ago

WP use wp_get_nocache_headers() to set no-cache headers for logged in users.

It seams, You can modify this header to control cashing in Surge. Example: somewhere in the theme's functions.php:

add_action( 'wp', function(){

    if( is_user_logged_in() ){
        header_remove( 'Cache-Control' );
    }
} );
MadtownLems commented 1 year ago

I tried using the header_remove() code snippet above, and it wasn't working properly for me. I tried nocache_headers() instead, and that seems to be working.

https://developer.wordpress.org/reference/functions/nocache_headers/