To optimize JavaScript in WordPress, don’t start by randomly disabling files. A page can load scripts for a form, a carousel, a map, a consent banner or an analytics plugin. Many of those functions aren’t needed on every page.
The right approach is to look at what actually loads, identify the plugin or theme responsible, then remove or defer only the nonessential scripts. That way you preserve useful features while reducing work for the browser.
Why target scripts instead of the whole site
A JavaScript file costs more than just download time. The browser also has to parse and execute it. When a regular script appears in the document without the attribute defer nor async, it can block HTML parsing while it loads and runs. The documentation on web.dev about resource loading explains this difference between blocking, async and deferred scripts.
The issue isn’t necessarily an entire plugin. For example, a forms plugin may be needed on your Contact page, but its script doesn’t have to be present on every blog post. Similarly, a gallery script can be useful on some listings or service pages without being loaded across the whole site.
This method complements plugin analysis, it doesn’t replace it. If you first need to determine which tool generates a resource, also see how to find WordPress plugins that slow down your site. Here, the goal is more specific: decide whether a JavaScript file should be present on a given page.
Inventory the scripts loaded by a page
Choose a representative page, such as the homepage, a post and a contact page. Then open your browser’s developer tools and check the Network tab. Reload the page, then filter requests by JS.
For each file, note its URL, approximate size and the domain hosting it. The file path often gives a first clue. A URL containing /wp-content/plugins/plugin-name/ usually comes from a plugin. A path under /wp-content/themes/theme-name/ points instead to the theme or a child theme.
Ask the right questions for each script
- What visible feature does this file support on this page?
- Is this feature useful to the reader at this moment?
- Does the same file load on pages where this feature doesn’t exist?
- Does the file belong to your theme, a plugin, or an external service?
- Does any other script depend on it?
Don’t judge a file solely by its name or size. A small script can be essential for the mobile menu, the cart, or a form. Conversely, several small scripts together can create a lot of work on load.
Focus first on the pages that matter to your visitors. If your effort is part of a broader improvement, the article WordPress Speed: How to Make Your Site Faster and More Enjoyable helps place this work among the other performance factors.
Linking a file to its WordPress handle
In WordPress, a script is normally added with a unique identifier, called a handle. . The function wp_enqueue_script() is used to register and enqueue those scripts while declaring their dependencies. This identifier is important because WordPress doesn’t remove a script by its URL: it removes it by its handle.
To find this handle, check the settings or documentation of the relevant plugin. You can also search the child theme or plugin files, in a test environment, for calls to The function wp_enqueue_script(). .
Keep a short decision sheet: observed URL, handle, pages where the script is required, pages where it isn’t, and the results of your tests. This record becomes valuable when a theme or plugin is updated.
Disable a script only where it’s not needed
Conditional disabling is often safer than global removal. WordPress provides wp_dequeue_script() to remove a script that has already been added to the queue. However, you must use the correct handle and run your code after the plugin or theme has added the script.
Here is a deliberately generic example to place in a child theme or a custom plugin. Replace my-form-script with the confirmed handle for your installation. Also adjust the condition to match your actual pages.
add_action( 'wp_enqueue_scripts', function() {
if ( ! is_page( 'contact' ) ) {
wp_dequeue_script( 'mon-script-formulaire' );
}
}, 100 );In this example, the form script remains loaded on the Contact page, but WordPress attempts to remove it elsewhere. Do not add this code to a third‑party plugin’s files: an update could overwrite it. Prefer a child theme or a plugin designed for your customizations.
Respect dependencies before removing a handle
A script can serve as the basis for another file. Therefore, removing a shared library or handle can break a menu, a modal, a date picker, or part of the checkout. First remove scripts that are specific to a clearly limited function, then check every interaction on the page.
Before making any code changes, create a recoverable copy. You can follow this method to create a WordPress backup before a risky change, then run your tests on a staging site when possible.
Use defer without changing the script order
When a script is still needed but does not need to run during document construction, defer can be a good option. Deferred scripts download during HTML parsing and then run when parsing is complete. They also preserve their order in the document, unlike async.
WordPress allows declaring a defer or async tag manually in The function wp_enqueue_script(). strategy. Its documentation states that WordPress accounts for the dependency tree and applies the most appropriate allowed strategy. This prevents the core from imposing a more restrictive strategy that could break dependencies.
wp_enqueue_script(
'theme-interactions',
get_stylesheet_directory_uri() . '/assets/js/interactions.js',
array(),
'1.0.0',
array(
'strategy' => 'defer',
'in_footer' => true,
)
);Do not add async to a script that depends on jQuery, another file, or a specific document element. An async script can run in an unpredictable order. Reserve it for truly independent files, after reading the vendor documentation and testing behavior on the devices your visitors use.
Check after each change
Handle one script or one rule at a time. Then clear any applicable caches, open a private window, and verify the modified page. Also test the mobile version, as many themes enable a different menu and behaviors there.
- Navigate through menus and submenus.
- Submit any forms on the page.
- Open accordions, galleries, modals, and videos.
- Check the cart, checkout, and variations if your site uses WooCommerce.
- Check the browser console for JavaScript errors.
Finally, compare load behavior before and after, page by page. A useful reduction removes an unneeded script or defers noncritical work without removing an expected interaction. This discipline helps you optimize WordPress JavaScript sustainably, even as your plugins and theme evolve.









