Prevent inline styles from being removed by Remove Unused CSS

By default, Remove Unused CSS will remove every stylesheet and inline styles after the optimization is applied. In some cases, you might want to preserve inline styles.

You can exclude inline styles from being removed by using the filter: rocket_rucss_inline_content_exclusions

Note: The inline style will still be included in the Used CSS. If you want to remove it from the Used CSS, please follow this method.

Helper plugin

The following helper will implement the filter, allowing you to preserve inline declarations in the HTML after the Remove Unused CSS optimization is applied.

📥  Download (.zip): WP Rocket | Exclude Inline Styles from Remove Unused CSS
Developers: You can find the code for this plugin on GitHub.

Heads up! Manual code edit is required before use!


To install the plugin please follow these steps:

  1. Download the helper plugin ZIP file.
  2. Customize the plugin's code:
    Choose one CSS selector from inside the <style> declaration, the more specific the better, this way only the inline style you want will be preserved in the HTML code. 
  3. Edit line 28, change '.yourSelector' by the class you'd like to preserve:

    To exclude multiple inline css declarations, copy the entire line into a new line for each style declaration you want you exclude:
    $inline_exclusions[] = '.secondSelector'; 
    $inline_exclusions[] = '.thirdSelector'; 
    $inline_exclusions[] = '.anotherSelector'; 
    Note: Wildcards do not work in this helper plugin. Instead you can use a partial match. For example, if you have a dynamically generated class like .stk-wuoy0oa where wuoy0oa is the dynamic part, you can use: .stk- to cover all cases.
  4. Edit line 28, change '.yourSelector' to '' to exclude all inline css at once. The line should look like:
    $inline_exclusions[] = '';
  5. Install the helper plugin on your site - go to Plugins → Add New → Upload plugin and select the zip file.
  6. Activate the plugin
  7. Clear WP Rocket's cache
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.