Prevent CSS files from being removed by Remove Unused CSS

By default, Remove Unused CSS will remove every CSS file from the HTML after the optimization is applied. In some cases, you might need to preserve CSS files.

You can exclude CSS files being removed by using the filter: rocket_rucss_external_exclusions

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

Helper plugin

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

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

Heads up! Manual code edit is required before use!

Installation

To install the plugin, please follow these steps:

  1. Download the helper plugin ZIP file.
  2. Customize the plugin's code:
    Copy the path of the CSS file you want to preserve, for example: /wp-content/plugins/plugin-name/css/my-specific-file.css

    If you are excluding files from a 3rd party domain, use the full URL: https://cloud.typography.com/6146554/6593832/css/fonts.css

    Edit line 29, change /wp-content/plugins/plugin-name/css/file.css by the class you'd like to preserve:
    $external_exclusions[] = '/wp-content/plugins/plugin-name/css/my-specific-file.css';

  3. To exclude multiple CSS files, copy the entire line into a new line for each style file you want to exclude:
    $external_exclusions[] = '/wp-content/plugins/plugin-name/css/my-first-file.css';
    $external_exclusions[] = '/wp-content/plugins/plugin-name/css/my-second-file.css';
    $external_exclusions[] = '/wp-content/plugins/plugin-name/css/my-third-file.css';
    		
  4. To exclude dynamically generated file, for example: /wp-content/uploads/uag-plugin/assets/0/uag-css-85-1696456733.css, don't use full file name and path, or wildcards since they don't work in this case. Use a partial matching string.
    Example: /wp-content/uploads/uag-plugin/assets/0/.
  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.