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.
- Download the helper plugin zip file, then un-zip it.
- Open the PHP file in a text editor.
- 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', ],
- 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.
- On line 45, when
exclude_from_all_pages
is set totrue
(default), exclusions will be applied for all pages. Set tofalse
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. - On line 50, Change
exclude_from_home
totrue
if you need to apply exclusions to the home page, the default value isfalse
. - 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 usemy-slider-page
like the following:
'specific_pages_to_exclude' => [ 'my-slider-page', ],
- Re-zip the folder.
- Install the helper plugin on your site - go to Plugins > Add New > Upload plugin and select the zip file.
- Activate the plugin.
- Clear the WP Rocket cache