How to disable reCaptcha v3 except on Contact Form 7 pages?

Contact form 7 is loading reCaptcha v3 scripts on all the pages of the sites which is making the website slow.

So I was using the script below which was working fine before two weeks from now, but now it stopped working and it's now loading even more scripts. Why could it be?

I don't want to use extra plugins.

function contactform_dequeue_scripts() {
    $load_scripts = false;
    if( is_singular() ) {
        $post = get_post();
        if( has_shortcode($post-post_content, 'contact-form-7') ) {
            $load_scripts = true;   
        }
    }
    if( ! $load_scripts ) {
        wp_dequeue_script( 'contact-form-7' );
    wp_dequeue_script('google-recaptcha');
        wp_dequeue_style( 'contact-form-7' );
    }
}
add_action( 'wp_enqueue_scripts', 'contactform_dequeue_scripts', 99 );

Topic captcha plugin-contact-form-7 plugin-development Wordpress

Category Web


I use Block editor and Contact form 7 block, and this is a very elegant solution for this requirement:

/**
 * Remove Contact form 7 recaptcha from each page, load only when block exists - works on version 5.5.6
 */

add_action( 'wp_enqueue_scripts', function() {
    if ( ! has_block( 'contact-form-7/contact-form-selector' ) ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_script( 'google-recaptcha' );
        wp_dequeue_script( 'wpcf7-recaptcha' );
        wp_dequeue_style( 'contact-form-7' );
    }
}, 101 );

Please do note that author claims this is not a good idea.


A neater version:

function wpcf7_dequeue_redundant_scripts() {
    $post = get_post();
    if ( is_singular() && !has_shortcode( $post->post_content, 'contact-form-7' ) ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_style( 'contact-form-7' );
        wp_dequeue_script( 'wpcf7-recaptcha' );     
        wp_dequeue_style( 'wpcf7-recaptcha' );
        wp_dequeue_script( 'google-recaptcha' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpcf7_dequeue_redundant_scripts', 99 );

I'd put the dequeue statements inside the 'if', replacing the $loadscripts line. No need to set the flag and then check the flag to dequeue. That might simplify the code for further debugging.

Edited: suggested code corrections:

function contactform_dequeue_scripts() {
    if (is_singular()) {
        $post = get_post();
        if (has_shortcode($post->post_content, 'contact-form-7')) {
            wp_dequeue_script('contact-form-7');
            wp_dequeue_script('google-recaptcha');
            wp_dequeue_style('contact-form-7');
        }
    }
}
add_action('wp_enqueue_scripts', 'contactform_dequeue_scripts', 99);

This only dequeue's scripts if there is a shortcode for CF7, and if it is a singular page. Otherwise, things are done normally. Easier to read the code and figure out what is happening.


This works for me.

function contactform_dequeue_scripts() {

    $load_scripts = false;

    if( is_singular() ) {
        $post = get_post();

        if( has_shortcode($post->post_content, 'contact-form-7') ) {
            $load_scripts = true;
            
        }

    }

    if( ! $load_scripts ) {
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_script( 'google-recaptcha' );
        wp_dequeue_script( 'wpcf7-recaptcha' );     
        wp_dequeue_style( 'wpcf7-recaptcha' );
        wp_dequeue_style( 'contact-form-7' );
        
    }

}
add_action( 'wp_enqueue_scripts', 'contactform_dequeue_scripts', 99 );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.