Optimizing Contact Form 7 – Learn WordPress from a to z wordpress tips

Tutorials 0 lượt xem

How to optimize Contact Form 7?

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

It will help WordPress have the ability to create a contact form and insert the form into any page you want.

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

It helps to optimize the web for speed and seo for the website.

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 your theme’s functions.php file

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;
}<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span>
</div>

The above code will check that if there is no  contact form 7 shortcode  , it will automatically  deregister css and js  to help optimize website loading speed effectively.

Epilogue

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

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

Follow fanpage to receive the latest posts:  Group

Wish you have interesting and interesting knowledge about wordpress!

Bài viết liên quan