How to Prevent WP Rocket from Resetting Settings in Safe Mode: A Deep Dive

September 17th, 2023



WP Rocket is renowned for its incredible ability to speed up WordPress websites. However, sometimes, for safety reasons, WP Rocket enters a mode called “Safe Mode,” resetting various optimization settings to their defaults. While this feature is meant to troubleshoot potential issues, some advanced users might find it counterproductive in specific scenarios as it deletes many of your cache settings, meaning you have to put in extra work just to have it run how it was previously. If you’re among them, this blog post is for you. Let’s unravel the mystery behind this and explore how to prevent WP Rocket from making changes in Safe Mode.

Understanding WP Rocket’s Safe Mode

Safe Mode in WP Rocket is a troubleshooting feature. When the plugin detects potential issues, it triggers this mode to ensure your website remains operational by reverting specific optimization settings. The idea is simple: by turning off certain features, it’s easier to pinpoint and solve the problem.

However, not everyone wants this automatic reset. For instance, if you’ve thoroughly tested all features of WP Rocket with your theme and plugins, and you’re confident in its stability, you might prefer to keep Safe Mode from altering any settings.

The Code Behind Safe Mode

Let’s start by examining a segment of WP Rocket’s code responsible for activating Safe Mode:

public function activate_safe_mode() {
    /**
     * Filters the array of options to reset when activating safe mode
     *
     * @since 3.7
     *
     * @param array $options Array of options to reset.
     */
    $reset_options = apply_filters(
        'rocket_safe_mode_reset_options',
        [
            'async_css'             => 0,
            'lazyload'              => 0,
            // ... other settings here ...
            'cdn'                   => 0,
        ]
    );

    $this->options->set_values( $reset_options );
    $this->options_api->set( 'settings', $this->options->get_options() );
}

The magic happens in the apply_filters function call. WordPress uses this function to allow developers to change values dynamically. The rocket_safe_mode_reset_options filter lets us modify the array of settings that get reset during Safe Mode activation.

Stopping Safe Mode from Making Changes

Thanks to the rocket_safe_mode_reset_options filter, we can dictate the behavior of Safe Mode. By providing an empty array to this filter, we can prevent WP Rocket from resetting any options.

Here’s how to do it:

function prevent_wp_rocket_safe_mode_changes( $reset_options ) {
    // Return an empty array to prevent any changes when safe mode is activated
    return array();
}

add_filter( 'rocket_safe_mode_reset_options', 'prevent_wp_rocket_safe_mode_changes' );

By adding the above code to your theme’s functions.php file or a custom functionality plugin, you’re instructing WP Rocket to keep its hands off your settings, even in Safe Mode.

Why Does This Work?

The concept is straightforward: the rocket_safe_mode_reset_options filter expects an array of settings to reset. In the default behavior, this array contains various optimization settings with their default values. However, when we hook into this filter and return an empty array, we’re effectively saying, “There’s nothing to reset.” As a result, WP Rocket won’t make changes to any settings during Safe Mode activation.

Conclusion

While Safe Mode is a handy feature of WP Rocket for troubleshooting, it’s not always desired. Thankfully, the flexibility of WordPress and WP Rocket allows us to tailor behaviors to our preferences. Remember, always test changes in a staging environment before deploying them to a live website. This ensures a smooth experience for your visitors. Safe optimizations!

Was this post helpful?