Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP Rocket + Super Page Cache for Cloudflare : Disabling WP Rocket Page caching for it to work with Super Page Cache for Cloudflare

WP Rocket (No Page Cache) - Must Use Plugin - Installation Guide

When using Super Page Cache for Cloudflare along with WP Rocket plugin it is highly recommended that you do not use more than one page caching system on your site as that will lead to compatibility issues and problems.

Whether you have a server level page caching system or some other page caching plugin installed in yoru site, you need to ensure that all page caching mechanisms are disabled on those plugin before using the Super Page Cache for Cloudflare plugin on your website. Also, ensure that there is no server rule in your .htaccess file or nginx.conf file which is modifying the cache-control header value of the page as that value needs to be managed by the Super Page Cache for Cloudflare plugin.

Here is what you should do if you are uising WP Rocket along with Super Page Cache for Cloudflare to get the best results.

Step 1 - Make sure you have a clean slate (Disable all caching plugins)

First make sure that both WP Rocket and Super Page Cache for Cloudflare are disabled on your wensite and inside your wp-config.php file there is no WP_CACHE constant. If you see a statement like define( 'WP_CACHE', true ); or something similar inside your wp-config.php file, please delete that line and save your wp-config.php

Step 2 - Install the mu-plugin given below

After this README documentation you will file another file named wp-rocket-no-cache.php file attached to it. Make a copy of that file in your system either by downloading the file or creating a file with the same name and copy poasting the content given below.

After that open your FTP client and go to /wp-content/mu-plugins/ folder. Inside /wp-content/ if you do not see the mu-plugins folder, just create a new folder and name it mu-plugins. Then inside this folder put the mu-plugin code given below.

Finally, when you go inside yoru WP Admin > Plugins section, now you should see that a new Must Use Plugin section has been added and under which this WP Rocket - No Cache plugin is mentioned. This will ensure that you have done this step right.

Step 3 - Enable WP Rocket & Disable page caching

Now enable the WP Rocket Plugin and make sure page caching or mobile device caching is disabled inside this plugin. After enabling WP Rocket, open the FTP cleint again and open wp-config.php file. This time there should not be any mention of the WP_CACHE constant.

Similarly, when you would go inside the /wp-content/ folder, there should not be any advanced-cache.php file as well.

You can use WP Rocket to optimizing your static files like CSS/JS etc. or you may use other plugins dedicated to static file optimization like Autoptimize. For image optimization, there is Cloudflare Polish (comes with Cloudflare Pro plan and above), Imagekit, Optimole, Shortpixel, EWWW Image Optimizer etc.

But this gist is for those who must need to use WP Rocket.

Step 4 - Enable & Setup the Super Page Cache for Cloudflare

Now it is the time to enable the Super Page Cache for Cloudflare plugin, enter the Cloudflare API details and enable page cache. After this, everything should work fine without any major issues.

<?php
/**
* Plugin Name: WP Rocket | Disable Page Caching
* Description: Disables WP Rocket’s page cache while preserving other optimization features.
* Plugin URI: https://gist.github.com/isaumya/d5990b036e0ed2ac55631995f862f4b8#file-wp-rocket-no-cache-php
* Author: Saumya Majumder & WP Rocket Support Team
* Author URI: http://wp-rocket.me/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace WP_Rocket\Helpers\cache\no_cache;
// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();
/**
* Disable page caching in WP Rocket.
*
* @link http://docs.wp-rocket.me/article/61-disable-page-caching
*/
add_filter( 'do_rocket_generate_caching_files', '__return_false' );
// Disable generating the advanced-cache.php file by WP Rocket
add_filter( 'rocket_generate_advanced_cache_file', '__return_false' );
// Disable WP Rocket Mandetory Cookies
add_filter( 'rocket_cache_mandatory_cookies', '__return_empty_array' );
// Prevent WP Rocket from changing the WP_CACHE constant
add_filter( 'rocket_set_wp_cache_constant', '__return_false' );
// Prevent WP Rocket from writing to the htaccess file
add_filter( 'rocket_disable_htaccess', '__return_true' );
// Disable other WP Rocket stuffs that are not needed and handelled by this plugin
add_filter( 'rocket_display_input_varnish_auto_purge', '__return_false' );
/**
* Cleans entire cache folder on activation.
*
* @author Arun Basil Lal
*/
function clean_wp_rocket_cache() {
if ( ! function_exists( 'rocket_clean_domain' ) ) {
return false;
}
// Purge entire WP Rocket cache.
rocket_clean_domain();
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\clean_wp_rocket_cache' );
@morrisseysteve
Copy link

morrisseysteve commented May 31, 2022

Hi, This is very clear. Thanks. One question. After re-activating Super Page Cache for Cloudflare there is now an advanced-cache.php file in wp-content. It appeared the second I re-activated SPCC. This is the expected behaviour, right? Thanks

@isaumya
Copy link
Author

isaumya commented May 31, 2022

Hi @morrisseysteve,
yes, if you view the code of that advanced-cache.php file you will see that the content of it is for Super Page Cache for Cloudflare plugin. That file will only get added if you have enabled Fallback Cache inside Super Page Cache for Cloudflare plugin. If fallback cache is disabled then there should not be any advanced-cache.php file.

@morrisseysteve
Copy link

morrisseysteve commented May 31, 2022

You're right, I have enabled fallback cache. I did look inside the advanced-cache.php file and it was indeed Super Page Cache content in there, so I wasn't overly concerned but I wanted to know for certain. Thanks for clearing it up, and so quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment