Contact Form 7 & Thesis : Stop Unneeded JS & CSS Files From Loading On Every Page

by DB on November 30, 2011


If you are using the Contact Form 7 plugin for WordPress then you should definitely try this optimization tip. By default the Contact Form 7 plugin loads its Javascript and CSS stylesheet on every page and post of a blog even if the contact form is not called. I have seen a lot of plugin developers do stupid things like this. This is absolutely unnecessary and adds to the load time of a webpage. With Google recently emphasizing the importance of Page speed, it has become even more necessary to stop unwanted scripts and stylesheets from loading.

In spite of this shortcoming of the Contact Form 7 plugin, I still like it for its simplicity and ease of use. I use it on some of my other wordpress installations as well. All my WordPress installs use the popular Thesis framework. Here is a simple custom function that can be added to the Custom_functions.php file of the Thesis framework. This code will stop the Contact Form 7 Javascript and stylesheet files from being loaded on every page.

/*code to remove loading of contact form 7 css and js on every page but the contact us page*/
  
function remove_contact_form_css_js() {
    if( ! is_page('contact-us') )
    {
        remove_action('wp_print_scripts', 'wpcf7_enqueue_scripts');
        remove_action('wp_print_styles', 'wpcf7_enqueue_styles');
    }
}

add_action('wp_head', 'remove_contact_form_css_js',1);

In the above code, replace ‘contact-us’ with the slug of your page where the Contact Form 7 is embedded. In the above code we call our function with the WordPress hook of wp_head. We check if the page is our contact us page and if it is not then stop loading the javascript and css files of the Contact Form 7.

The above tweak will result in faster loading pages and a better YSlow score and Google PageSpeed score.

{ 2 comments… read them below or add one }

1 wiki.answers.com September 6, 2015 at 12:51 pm

Awesome site you have here but I was curious if you
knew of any message boards that cover the same topics discussed in this article?
I’d really like to be a part of group where I can get suggestions from other
knowledgeable individuals that share the same
interest. If you have any suggestions, please let me know.
Thanks!

2 Latrice February 8, 2016 at 1:00 pm

El punto en cointra en este tipo de préstamos personales son los honorarios, que acostumbran a ser altísimos.

Leave a Comment