How to programmatically configure WP Rocket's options

Heads up! This guide assumes comfort with editing PHP code. If that's not you, please ask your developer to implement it for you.

There are currently 2 possible ways to automate the configuration of WP Rocket:

  • Using the WP Rocket CLI can be a way to automate the configuration of WP Rocket.
  • Alternatively, you can modify the settings via PHP using our function update_rocket_option ()

Using update_rocket_option()

  1. To do this, you will create a PHP file and upload it to the root of your WordPress installation, where wp-load.php is located. 
  2. The format of the contents will be like this:
  3. <?php 
    // Load WordPress.
    require( 'wp-load.php' );
    
    if(function_exists('update_rocket_option')) {
      update_rocket_option( 'option_name', 0);
    }
    	
  4. option_name will be replaced with the appropriate name from the list below. Add one line for each option that you want to configure - you can add as many as needed. 
  5. Specify 0 to turn the option off, 1 to turn it on
  6. Here is an example of a configuration file which turns off LazyLoad, turns on Delay JavaScript Execution, turns on the CDN and sets the CNAME to example.com:
  7. <?php 
    // Load WordPress.
    require( 'wp-load.php' );
    
    if(function_exists('update_rocket_option')) {
      update_rocket_option( 'lazyload', 0);
      update_rocket_option( 'delay_js', 1);
      update_rocket_option( 'cdn', 1);
      update_rocket_option( 'cdn_cnames', ["example.com"] );
    }
    	
  8. Visit the file in your browser to execute the configuration.
  9. When you visit your admin, you will see that the options have been applied per your configuration file.

If you are using this to set an initial configuration, you only need to update the options which will differ from the default configuration. All others will remain at default, or unchanged from their current values. Once set, all options can be re-configured directly from the settings page. The configuration file will only be applied after being visited in your browser.

Here is a list of commonly used WP Rocket features and their associated option names:

Mobile Cache cache_mobile
Specific cache for mobile do_caching_mobile_files
WebP Cache cache_webp
User Cache cache_logged_user
Disable Emojis emoji
Minify CSS minify_css
Minify JavaScript minify_js
Combine JavaScript minify_concatenate_js
Load JavaScript Deferred
defer_all_js
Delay JavaScript Execution
delay_js
Optimize CSS Delivery async_css
Remove Unused CSS
remove_unused_css
CDN cdn
Set CDN CNAMES
'cdn_cnames', ["example.com"]
LazyLoad for Images
lazyload
LazyLoad for iframes lazyload_iframes
LazyLoad - replace YouTube thumbnail lazyload_youtube

Find a complete list of all options here.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.