Menu Close


Contact Form 7 – reCaptcha only on contact pages

Update on December 16, 2020:

This is now deprecated. All you have to do now is add some custom CSS to your theme.

Many people use the Contact Form 7 plugin on their WordPress sites. It can be integrated with Google’s reCaptcha service to prevent spam, but this significantly increases page loading time, because the plugin adds the reCaptcha script to every page. Enter

This code added to the functions.php file in your theme will make the script load only on contact pages:

function manage_cf7_js_styles_recaptcha() {
// Dequeue cf7 and recaptcha scripts and styles, preventing them from loading everywhere
wp_dequeue_script( ‘contact-form-7’ );
wp_dequeue_script( ‘google-recaptcha’ );
wp_dequeue_style( ‘contact-form-7’ );

// If current post has cf7 shortcode, enqueue!
global $post;
$poststoshow = [16];
if ( isset( $post->post_content ) AND ( in_array($post->ID, $poststoshow, true ) OR has_shortcode( $post->post_content, ‘contact-form-7’ ) ) ) {
if ( function_exists( ‘wpcf7_do_enqueue_scripts’ ) ) {
wp_enqueue_script( ‘google-recaptcha’ );
add_action( ‘wp_enqueue_scripts’, ‘manage_cf7_js_styles_recaptcha’, 10, 0 );

This decreased the loading time of my main page by another half a second and increased my Pagespeed score to 96. Thanks, Thilo!