Manchester Web Design | SquareNet Media
Single Blog Post

Restricting Contact 7 to Only Load its CSS and JS on Selected Pages

by SquareNetMedia Web Developer

Posted on November 26, 2016 at 10:18 AM

Contact 7 can manage multiple contact forms, plus you can customise the form and the mail content flexibly with simple markup.

However to speed up your wordpress website you do not want to load the Contact 7 CSS and Javascript until the user is on a page which the contact form is on.

This little guide is going to show you how to achieve this….

Styles and scripts are always set up by the functions wp_enqueue_script() and wp_enqueue_style(), which have to be tied to a particular action hook in order to function. I took a peek inside Contact Form 7, and it looks like it’s using action tags of wpcf7_enqueue_scripts and wpcf7_enqueue_styles to add them to the wp_print_scripts and wp_print_styles hooks.

So, what you need to do is un-hook the scripts and styles from every page but your contact page. The wp_head action fires before the script and styles actions, so you’ll need to add something like this to your theme’s functions.php file:

function remove_wpcf7_extras() {
remove_action('wp_print_scripts', 'wpcf7_enqueue_scripts');
remove_action('wp_print_styles', 'wpcf7_enqueue_styles');

if( ! is_page('contact me') ) {
add_action('wp_head', 'remove_wpcf7_extras');

The is_page() function will return true when you’re on the contact page (assuming the name is “contact me”) … you can also use the page slug and page ID for the filter. On all other pages, the if() conditional will add the script/style removal function to the wp_head action, which fires just before the wp_print_scripts and wp_print_styles actions.

About the Author


We are a small team based in Manchester, UK and have been around for over 15 years. We are passionate about web design and development. We have experience with eCommerce, Wordpress, Magento, Search Engine Optimisation, Web Hosting, Google Analytics, HTML, jQuery and much more.


How to Add A Favicon To Your WordPress Website

Posted on Dec 24 2016 at 1:07 pm

Favicons, short for favorite icons, are a small but significant part of branding a website. [...]

Read More

How to Auto Link Keywords in Post Content And Excerpt

Posted on Dec 24 2016 at 12:26 pm

If you are a blogger and make some cash from affiliate marketing then you probably [...]

Read More

Packaging Plugins With Your WordPress Theme Using TGM Plugin Activation Library

Posted on Nov 20 2016 at 1:28 pm

When you’re shipping your theme for sell, it’s often required to bundle the required plugins. [...]

Read More