Optimize Contact Form 7

Optimize Contact Form 7

[ad_1]

How to optimize Contact Form 7?

Contact Form 7 is the most popular and comprehensive free form builder plugin for WordPress.

This will help WordPress create a contact form and insert the form into any page you want.

The optimal is that only when users access pages/posts that use the contact form, the website loads the resources related to it. As for pages without contact form, it will not load js, css.

It helps in optimizing the web for website speed and SEO.

Let’s start with optimization:

I will share with you how to hide the JS or CSS of this plugin when the form does not exist in any page or post.

You just need to paste the above code into the file theme functions.php

add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
add_action( 'the_content', 'load_cf7_assets' );
function load_cf7_assets($content){
	global $post;
	$post_content = $post->post_content;
	if ( has_shortcode( $post_content, 'contact-form-7' ) ) {
		// Load CF7 Javascript
		if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
			wpcf7_enqueue_scripts();
		}
		//  Load CF7 CSS
		if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
			wpcf7_enqueue_styles();
		}
	}
	return $content;
}

The above code will check if it is not there contact form shortcode 7 then it will be automatically unregister css and js Help to optimize website loading speed effectively.

Epilogue

With just the simple code above, you have somewhat optimized your wordpress website, helping users to have a better experience.

If you find it interesting, you can follow the wordpress tips section to learn more new knowledge.

Follow the fanpage to receive the latest articles: Hocwordpress Group

I wish you to have interesting and interesting knowledge about wordpress!

0
0
voice

Rate items



[ad_2]

Recommended Posts