Avoid loading Contact form 7 on all pages

Hosting Mautic QuickStart -50% with support in Spanish
24 hours and free training

Send up to 1,000,000 emails/year. Mautic support in Spanish 24 hours, 365 days

Contact Form 7 is one of them and this is due to its simplicity in the installation, configuration and creation of forms but like most plugins it has its “bad things” in terms of performance.

The reason is because load your CSS, JavaScript and Ajax files on every page by default, which may affect website performance and it can even cause problems if the server we work with has low response times.

Surely you are wondering why the developers do it that way and do not implement it from the beginning so that it loads only where it is needed. The reason is very simple, because They have no way of knowing where we are going to use it.

For example, in Contact Form 7 we can use it on the contact page but we can also use forms for products or even within an entry to request information.

In this post we will see how can we avoid uploading those files if they are not needed and that it is worth it.

1.- Disable JS and CSS on all web pages

We have two ways to disable Contact Form 7 on our installation.

1.- From the wp-config file that we find in the root of our WordPress installation

If we use WePanel we access the:

We access and to the folder where we have the installation.

See also  I can't see the chat tawk on my mobile

We select the wp-config file with the button on the right and from the menu we select » edit ».

At the end of the file we add the following you define.

define( ‘WPCF7_LOAD_JS’, false ); define( ‘WPCF7_LOAD_CSS’, false );

2.- From the functions.php file we find the route wp-content -> themes -> Name_Template

For me this is the best option if we work with a .

In this case we will have to access the folder where we have our template and just as we did before with the button on the right you select the file functions.php and we edit it.

At the end of the file we add the following filters:

add_filter( ‘wpcf7_load_js’, ‘__return_false’ ); add_filter( ‘wpcf7_load_css’, ‘__return_false’ );

With this we have managed to stop the loading of those files but of course we need to load them on the pages where we have forms and that is what we are going to do in the next step.

2.- Load the files on the pages where we have forms.

Let’s suppose that we have a form on the ‘ Contact ‘ page and another on the ‘ Home ‘ page, we would only have to add the following function in the functions.php file

function we_carga_contactform7(){ if ( is_page(‘contact, start’) ) { if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) { wpcf7_enqueue_scripts(); } if ( function_exists( ‘wpcf7_enqueue_styles’ ) ) { wpcf7_enqueue_styles(); } } } add_action( ‘wp_enqueue_scripts’, ‘we_carga_contactform7’ );

If you realize in the code we have a condition in which we indicate the pages where you have to upload the files, in this case it would be on the contact page and on the home page » if ( is_page(‘contact, home’) ) «.

See also  Put in font-weight 600 the titles of the payments

We can add the pages we need by simply adding the page slug and separating them by commas.

And with this we would have finished, they are two very simple steps that will help us optimize our website.

Conclusions

The optimization of our website is very important so that Google loves us more and eats us with kisses, without forgetting the visitors of our website who will see an improvement in the web load.

Any micro seconds that we can cut to the load, are points that we earn and speed is an important factor for a better visibility of our Brand.

Help us improve our content by sharing your opinion

.com content team
CMS Support

Loading Facebook Comments ...
Loading Disqus Comments ...