Programmatically toggle WP Rocket options under specific conditions

If you need to programmatically deactivate or activate one of WP Rocket's options, you can use our pre_get_rocket_option_{option_name} filters.

This can be particularly useful in some cases. Imagine that you need to deactivate the Delay JavaScript Execution in 100 Product pages. This is a good use case for the following helper plugin. 

In this particular case, the helper plugin will check if the current page is a "product" singular, and deactivate the Delay JavaScript Execution option. 

This helper plugin is very flexible. You can modify it to suit your particular needs using WordPress Conditional Tags
Here you can find the full list of all the available  pre_get_rocket_option_ filters: List of pre_get_rocket_option_{option_name} filters

  • To deactivate an option use __return_zeroadd_filter( 'pre_get_rocket_option_delay_js', '__return_zero' );
  • To activate an option use __return_trueadd_filter( 'pre_get_rocket_option_delay_js', '__return_true' );

Helper plugin

๐Ÿ“ Manual code edit required before use!

๐Ÿ“ฅ   Download (.zip): WP Rocket | Conditionally toggle options
Developers: You can find the code for this plugin on GitHub.

  1. Download the helper plugin zip file, then un-zip it.
  2. Open the PHP file in a text editor.
  3. Edit line 25. Replace if ( is_singular( 'product' ) ) with any other condition. You can find some examples below. Please check this article to get more ideas,
  4. Edit line 28: change add_filter( 'pre_get_rocket_option_delay_js', '__return_zero' ); to use any filter from our list of pre_get_rocket_option_{option_name} filters available. If you want to add more filters, you can just duplicate line 28 and add another line.
  5. Re-zip the folder.
  6. Install the helper plugin on your site - go to Plugins > Add New > Upload plugin and select the zip file.
  7. Activate the plugin.

Examples

Activate Lazyload on a specific category
Reference: is_category()

if ( is_category('9' ) ) { 
   add_filter( 'pre_get_rocket_option_lazyload', '__return_true' ); 
}

Below you can find more examples of is_category(). You can find them on the WordPress Codex reference page above:

is_category();
// When any Category archive page is being displayed.

is_category( '9' ); 
// ID based, When the archive page for Category 9 is being displayed.

is_category( 'Stinky Cheeses' ); 
// Name based: When the archive page for the Category with Name "Stinky Cheeses" is being displayed.

is_category( 'blue-cheese' ); 
// Slug based: When the archive page for the Category with Category Slug "blue-cheese" is being displayed.

is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ); 
// Multiple categories: returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses".


Deactivate Combine JS for Logged In Users
If you have enabled WP Rocket's  User cache, and for some reason, you need to manipulate some options for logged-in users you can do it.
Reference: is_user_logged_in()

if ( is_user_logged_in( ) ) { 
   add_filter( 'pre_get_rocket_option_minify_concatenate_js', '__return_false' ); 
}

Deactivate Defer JS exclusions on a specific custom post type
You can even remove DeferJS exclusions on a Custom Post Type. Imagine you want to defer every JS file only on a Custom Post Type called "Books" while keeping exclusions working on the other sections of your website.
To achieve that, you can remove all Defer JS exclusions for the "Books" CPT.
Reference:  is_singular()

if ( is_singular( 'books' ) ) { 
  add_filter( 'pre_get_rocket_option_exclude_defer_js', function() {
    return array();
  });
}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.