Exclude Your CDN From Pages With SSL

Sometimes using your CDN for pages served via SSL can cause issues if your CDN does not serve assets via HTTPS.

Fix for WP Rocket v2.5+

In v2.5 of WP Rocket we included a very simple way to disable your CDN on pages with SSL.

Just go to WP Rocket → CDN and check the box Disable CDN on HTTPS pages:

Settings page with option to disable CDN for HTTPS pages

Fix for WP Rocket v2.4

This snippet allows you to exclude your CDN from those pages. Simply add the following code to the functions.php file of your theme and then clear the WP Rocket cache:

add_action( 'plugins_loaded', '__deactivate_rocket_cdn_on_ssl' );
function __deactivate_rocket_cdn_on_ssl() {
    if( is_ssl() ) {
        define( 'DONOTCDN', true );
    }
}

Or, for better maintainability you can also fix this by creating a must-use (mu) plugin:

1) Create a directory mu-plugins in wp-content
2) Using a basic text editor make a new file and paste the following code:

<?php 
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
/*
Plugin Name: Deactivate CDN on SSL pages with WP Rocket
Author: Jonathan (WP Rocket Team)
Author URI: http://wp-rocket.me
*/
add_action( 'plugins_loaded', '__deactivate_rocket_cdn_on_ssl' );
function __deactivate_rocket_cdn_on_ssl() {
    if( is_ssl() ) {
        define( 'DONOTCDN', true );
    }
}

3) Save the file as: wprocket-fix.php (actually you can name it anything you want, this is just a suggestion )
4) Upload this file to the mu-plugins folder
5) Finally, you should clear the WP Rocket cache.

Fix for versions of WP Rocket earlier than 2.4

Simply add the following code to the functions.php file of your theme and then clear the WP Rocket cache:

add_action( 'init', '__deactivate_rocket_cdn_on_ssl_pages' );
function __deactivate_rocket_cdn_on_ssl_pages() {
	if ( is_ssl() ) {
		remove_filter( 'template_directory_uri'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_get_attachment_url'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'smilies_src' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'stylesheet_uri'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_minify_css_url' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_minify_js_url' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'bwp_get_minify_src' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'the_content', 'rocket_cdn_images', PHP_INT_MAX );
		remove_filter( 'widget_text', 'rocket_cdn_images', PHP_INT_MAX );
		remove_filter( 'style_loader_src', 'rocket_cdn_enqueue', PHP_INT_MAX );
		remove_filter( 'script_loader_src', 'rocket_cdn_enqueue', PHP_INT_MAX );
	}
}

Or, for better maintainability you can also fix this by creating a must-use (mu) plugin:

1) Create a directory mu-plugins in wp-content
2) Using a basic text editor make a new file and paste the following code:

<?php 
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
/*
Plugin Name: Deactivate CDN on SSL pages with WP Rocket
Author: Jonathan (WP Rocket Team)
Author URI: http://wp-rocket.me
*/
add_action( 'init', '__deactivate_rocket_cdn_on_ssl_pages' );
function __deactivate_rocket_cdn_on_ssl_pages() {
	if ( is_ssl() ) {
		remove_filter( 'template_directory_uri'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_get_attachment_url'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'smilies_src' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'stylesheet_uri'	, 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_minify_css_url' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'wp_minify_js_url' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'bwp_get_minify_src' , 'rocket_cdn_file', PHP_INT_MAX );
		remove_filter( 'the_content', 'rocket_cdn_images', PHP_INT_MAX );
		remove_filter( 'widget_text', 'rocket_cdn_images', PHP_INT_MAX );
		remove_filter( 'style_loader_src', 'rocket_cdn_enqueue', PHP_INT_MAX );
		remove_filter( 'script_loader_src', 'rocket_cdn_enqueue', PHP_INT_MAX );
	}
}


3) Save the file as: wprocket-fix.php (actually you can name it anything you want, this is just a suggestion )
4) Upload this file to the mu-plugins folder
5) Finally, you should clear the WP Rocket cache.