How to optimize Contact Form 7

If, like millions of WordPress users, your contact forms plugin is Contact Form 7 you may have noticed that of the only thing that sins is that according to what situations can slow down the loading of your website so we will put solution .

Selectively load Contact Form 7

] The main problem as far as the load is concerned, which has the plugin Contact Form 7 and the majority of plugins is that loads all its styles and scripts globally in your web , it does not matter if there is a form or not on each page.

This, which is understandable, because the developer does not know where you are going to put a form it is a burden on the load of pages and entries that will not show any form but that, however, will be uploaded n them all CSS and JavaScript plugin.

Imagine, then, a website with 20 pages and 500 entries in which only one of the pages has a form. What you will get is that the styles and scripts of Contact Form 7 will be loaded in all of them, it does not matter if they have a form or not .

The solution is to register styles and scripts only in the pages or entries in which there is form.

Do not register CSS styles of Contact Form 7

Not to register ( deregister ) the styles of Contact Form 7 in any page or entry other than in which yes there is a form we just have to know the slug of the page with form, and cancel them in the rest, like this:

This code must be put in your customizations plugin or at the end of archi vo functions.php of the active theme (better if it is the theme child ).

What the code does is check if the page is not the one with the slug ] contact in which case un-registers the Contact Form 7 styles using the WordPress function wp_deregister_style.

Not registering the Contact Form 7 JavaScript

] For to un-register the JavaScript in the pages that do not have form we will do almost the same, but this time with the function wp_deregister_script . Following the previous example would be like this:

Of course, remember to save changes in your plugin customizations or file functions.php the one you have chosen to add these functions.

What if I have forms on several pages?

Good question. No problem, then we will adapt the code for to include the slugs of all the pages that have form .

The only thing is to modify the line that looks for the condition is_page and that modify it indicating the array of slugs instead of a single one. For example:

How to locate the slugs of your pages with forms

If you do not know where to locate the slugs of your pages you just have to go to the window of administration of posts or pages and click in any of them in Quick Edition and copy from there the slug .

Selective Load of Contact Form 7 locating the shortcode

As you already know, the Contact Form 7 plugin will show your forms where you find a shortcode containing contact-form-7 so other mediant mode e that we can load styles and scripts is locating this string in your content and unregistering styles and scripts in the rest .

You just have to create a function like this:

What the above code does is override the actions by which Contact Form 7 loads styles and scripts there where the shortcode of the plugin.

Do not load anything from CSS or JavaScript from Contact Form 7

If, for any reason, at some point you want to completely cancel the loading of Contact Form 7 styles and scripts you can define new constants in your file wp-config.php these:

This may be interesting in development environments, for example.

Can it be done with plugins?

I'll say yes, but the issue is that the only one available for this basically what you are asking for is the conditional on which it will not apply which you have to know and apply, so it's the same as what we did with the first codes.

If you want to try it, it's called Script Logic .

The problem with the refill of Contact Form 7

A few months ago it arose [19459] 067] an optimization problem whereby the usual web speed meters like GTMetrix showed important page load delays because of the following string:

The problem occurred where you had a loaded cache of page with some plugin so the active content as the integrated reCAPTCHA of Contact Form 7 needs to be updated in each page load, generating a call to the famous refill .

If you still have this problem can be solved in two ways:


Since version December 2017, this problem has been solved used, so update.

Manually delete the plugin page cache

Although you already know that I never recommend modifying plugins, themes or WordPress files, if for another reason you can not update, and do not use any type of CAPTCHA, locate the includes / controller.php file and cancel (comment) or delete these lines:

And why better not change plugins?

You yourself, but Contact Form 7 is the plugin with a more continuous development, which is updated to improve, and with a wider ecosystem, due to the hundreds of extensions that improve it .

The only thing is that if it causes you some problem of delay of load of pages you already know how to solve it.

Loading …

That may also help you: