Stop plugins like Contact Form 7 loading their CSS and JavaScript on every page when they're only used on one.
add_action( 'wp_enqueue_scripts', function (): void {
// Pages that actually use the plugin, by slug.
if ( is_page( [ 'contact', 'quote' ] ) ) {
return;
}
// Contact Form 7's handles. Change these for other plugins.
wp_dequeue_style( 'contact-form-7' );
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_script( 'swv' );
}, 100 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Many plugins load their files on every page “just in case”. A contact form plugin used only on your Contact page still adds CSS and JavaScript to your homepage, blog and shop. This snippet removes them everywhere except where they’re needed.
The callback runs late on wp_enqueue_scripts, after plugins have queued their files. On the pages you list it does nothing; everywhere else it dequeues the plugin’s style and scripts by handle.
id="contact-form-7-css" has the handle contact-form-7. The Query Monitor plugin lists them all.Open your homepage source and search for contact-form-7. It shouldn’t appear. Then check the contact page still loads the form correctly and submits.