Home » WordPress » How to load recaptcha script with Contact Form 7 only where necessary

How to load recaptcha script with Contact Form 7 only where necessary

Are you using Contact Form 7 plugin with recaptcha in your WordPress website? If the answer is yes, you probably noticed Google recaptcha badge on every single page of your site.

The latest Google Recaptcha is great – it makes a superb job securing your email from spam which you would get from website contact forms. But loading recaptcha script on every single page (even if it does not have a contact form in it) does not make much sense, and it does affect website loading speed.

So here you’ll find a simple but effective solution how to automatically load recaptha script only on pages where contact form really is.

How to load recaptcha for Contact Form 7 only on pages where contact form exists

By default Contact Form 7 loads recaptcha on every single WordPress post or page. It shows the recaptcha badge, and loads an external script that increases website loading time (not by much, but it still adds up).

Here’s what you need to do to avoid it:

  • Open WordPress admin panel, and go to Appearance >> Theme editor.
  • Click on theme’s functions.php file, and add this function to the end of the file:
add_action('wp_print_scripts', function () {
	global $post;
	if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'contact-form-7') ) {
		wp_dequeue_script( 'google-recaptcha' );
		wp_dequeue_script( 'wpcf7-recaptcha' );

Here’s how it should look like:

How to load recaptcha script with Contact Form 7 only where necessary

Best WordPress hosting 2024

Don’t forget to save changes (click on Update File button).

The function is quite simple and straightforward – it checks if page has a contact form 7 shortcode in it. If shortcode is not found, function removes Google recaptcha script from the script queue.

Did you find this tutorial useful?
Don’t forget to share it!

Fathom analytics - privacy focused cookie-free website analytics

Most popular tutorials

Get our latest WordPress news and special offers from RockSolidWP!

Only useful WordPress and WooCommerce tips and tricks and exclusive offers for our readers once a month. No marketing nonsense.

Looking for reliable yet affordable WordPress hosting?
Hostinger is the way to go!

Get 10% OFF by using code IMAKEITWORK