With 5+ million downloads from the official WordPress Plugins directory, it’s obvious that Contact Form 7 is a very popular contact form building plugin. But did you know it loads CSS and JS code on every single page of your website? Even pages where a contact form doesn’t even exist! In an era where speed has become an SEO ranking factor it’s important to keep your pages lean and page loads time fast.
In this tutorial I’m going to show users of Contact Form 7 how to speed up their WordPress website by removing the CSS and JS file on pages that do not include the contact form.
De-register Contact Form 7 CSS
The code snippet below will remove the Contact Form stylesheet from all pages except our Contact page. It first determines of the function wpcf7_enqueue_styles exists and then if the page is not Contact it will de-register the stylesheet.
De-register Contact Form 7 JS
Use the code snipet below to remove the JS file from pages that don’t contain our contact form. The WordPress function is_page is being used to determine which page the code belongs on, and will remove it from all others.
Details of Function is_page()
In both examples above I’m passing the slug name of ‘contact’ to the function is_page(). Simply replace my slug with yours. It’s that simple.
If you need to include the CSS and JS on multiple pages, simply pass an array into the is_page() function.