Key points
- Wrap wp_enqueue_script() in is_*() conditionals: a script a page does not use should not be loaded at all.
- For plugins, remove their files with wp_dequeue_script() on a late-priority hook.
- Since WordPress 6.3, ‘strategy’ => ‘defer’ keeps scripts from blocking rendering without hacking the tags.
Open the Network tab on a slow WordPress site and you will usually find the same pattern: a Privacy Policy page pulling in the contact form’s JavaScript, the homepage slider library, and the CSS for a configurator that lives on exactly one URL. None of these files is heavy on its own. Together they delay rendering and keep the browser busy, which hurts most on mid-range phones. WordPress already gives you everything needed to load a file only where it is used.
In your theme: make enqueues conditional
Most themes register every script in a single wp_enqueue_scripts callback. Adding a condition is all it takes. Template tags like is_front_page(), is_singular( 'product' ), is_page_template() and is_post_type_archive() all work at that point, because the main query has already run.
add_action( 'wp_enqueue_scripts', function () {
if ( ! is_page_template( 'templates/simulateur.php' ) ) {
return;
}
wp_enqueue_script(
'wts-simulateur',
get_theme_file_uri( 'assets/js/simulateur.js' ),
array(),
wp_get_theme()->get( 'Version' ),
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
} );
The fifth argument accepts an array since WordPress 6.3. 'strategy' => 'defer' adds the defer attribute while respecting dependencies, which is more reliable than the script_loader_tag filters many themes still use and that occasionally scramble execution order. Using the theme version as the asset version also takes care of stale browser caches after a release.
In plugins: remove what loads everywhere
Some plugins load their assets on every page because they cannot know where you will use them. Contact Form 7 is the textbook case. You can remove them with wp_dequeue_script() and wp_dequeue_style(), as long as your code runs after the plugin’s, hence priority 100.
add_action( 'wp_enqueue_scripts', function () {
$post = get_post();
$has_form = is_singular() && $post && (
has_shortcode( $post->post_content, 'contact-form-7' )
|| has_block( 'contact-form-7/contact-form-selector', $post )
);
if ( ! $has_form ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}, 100 );
To find the right handles, Query Monitor lists every script and stylesheet on the current page along with the component that added it. It is the first thing we install when auditing a site.
The trap we keep running into: the form is not always in the post content. When it sits in a footer widget, a popup or a hard-coded page template, the condition above breaks it without any visible error. Test every form on the site after the change, and actually submit one.
Blocks: let WordPress do the sorting
For custom blocks, declare assets in block.json (viewScript, style) and WordPress will only enqueue them when the block is on the page. Classic themes, however, get all core block styles bundled into one stylesheet by default. One line changes that:
// Classic theme: only print the CSS of core blocks actually used on the page
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
Block themes already load core block styles separately.
Check the result
After each change, compare the Network tab before and after on a handful of page types: home, post, a page with a form, a plain content page. Clear your optimization plugin’s cache first, since file concatenation can hide the difference. On our projects this cleanup usually fits in half a day, and it often does more for mobile experience than moving to a bigger server.
FAQ
Can removing a script break the site?
Yes, if something else depends on it. That is why we only remove files we have identified, page type by page type, and test the related features on staging before going live.
Doesn’t my optimization plugin already handle this?
Some let you disable assets per page, but through a settings screen that becomes hard to reason about over time. A rule in the theme is versioned, readable, and survives a plugin switch.
Is your WordPress site loading more than it shows? We run performance audits and clean up asset loading without touching your features.



