Exclude scripts from Load JavaScript Deferred for all pages or specific URLs

This helper will by default allow you to apply JavaScript files exclusions for Load JavaScript deferred to all pages if no specific page(s) is specified, or apply exclusions only to specified pages.

This can be useful in some cases where you can defer JavaScript files globally, but you need to also  exclude some scripts from being deferred only on some pages.

For example, when you have a slider that is not loading on your 2 or more pages but not the entire site.

๐Ÿ“ Manual code edit required before use!

๐Ÿ“ฅ   Download (.zip): WP Rocket | Exclude Scripts from Defer JS
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 29, Uncomment (remove //) and edit the line as needed to exclude external files. To exclude multiple files, copy the entire line into a new line for each file you wish to exclude. For example, if you need to exclude the external script www.example.com/my-slider-page/wp-content/plugins/my-plugin/js/slider.js, only use /wp-content/plugins/my-plugin/js/slider.js, like the following:

    'external_file_exclusions' => [
    	'/wp-content/plugins/my-plugin/js/slider.js',
    ],
  4. Edit line 38, Uncomment (remove //) and edit the line as needed to exclude inline JS. To exclude multiple inline JS scripts, copy the entire line into a new line for each inline script. Specify a unique string from an inline script to exclude it.
  5. On line 45, when exclude_from_all_pages is set to true (default), exclusions will be applied for all pages. Set to false line 45 and edit line 57 (step 7) if you would prefer to apply exclusions only to specified pages such as the page with sliders.
  6. On line 50, Change exclude_from_home to true if you need to apply exclusions to the home page, the default value is false.
  7. On line 57, Uncomment (remove //) and edit the line if you need to specify only specific pages to apply exclusions. To specify multiple pages, copy the entire line into a new line and specify a slug for each page. For example, if you need to exclude the external or inline script on https://www.example.com/my-slider-page, only use my-slider-page like the following:

    'specific_pages_to_exclude' => [
    	'my-slider-page',
    ],
  8. Re-zip the folder.
  9. Install the helper plugin on your site - go to Plugins > Add New > Upload plugin and select the zip file.
  10. Activate the plugin.
  11. Clear the WP Rocket cache
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.