• Resolved txtw

    (@txtw)


    Hi,

    Contact Form 7: 5.4
    ReCaptcha v2 for Contact Form 7: 1.3.2
    WordPress: 5.6.2

    ReCaptcha version set to v2, source google
    integration fine (worked before the update)

    I see that iqfix_wpcf7_recaptcha_form_tag_handler hooked to wpcf7_init and called before iqfix_wpcf7_recaptcha_enqueue_scripts registers the script in:
    wp_register_script( ‘google-recaptcha’, $url, array( ‘wpcf7-recaptcha-controls’ ), ‘2.0’, true );

    And iqfix_wpcf7_recaptcha_form_tag_handler tries to enque google-recaptcha script so first checks if it is registered in this if statement
    if ( ! wp_script_is( ‘google-recaptcha’, ‘registered’ ) && function_exists( ‘wpcf7_recaptcha_enqueue_scripts’ ) ) {
    wpcf7_recaptcha_enqueue_scripts();
    }
    it has not been registered that time so evaluates true and executes
    wpcf7_recaptcha_enqueue_scripts();
    from wpcf7 which tries to load V3 with the V2 site key that results in a 400 Bad Request error.

    If I register with iqfix_wpcf7_recaptcha_enqueue_scripts before the if statement or call iqfix_wpcf7_recaptcha_enqueue_scripts in the if statement it loads the V2 without error.

    I do not know if there is a local problem on my side or there is a bug in the plugin, so any feedback is appriciated.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author IQComputing

    (@iqcomputing)

    Hello,

    Could you link the website where you’re having this issue?

    The latest version of Contact Form 7 increased their hook priority from 10 to 20:

    add_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20, 0 );

    This plugin, as of 1.3.2, removes that hook via:

    
    // reCaptcha Enqueues
    remove_action( 'wp_enqueue_scripts', 'wpcf7_recaptcha_enqueue_scripts', 20 );
    add_action( 'wp_enqueue_scripts', 'iqfix_wpcf7_recaptcha_enqueue_scripts', 9 );
    

    The latest update may solve this issue but since the fix was already in 1.3.2 we’re not so sure. Are there any other plugins trying to add reCaptcha to the site anywhere?

    Thread Starter txtw

    (@txtw)

    Removed the plugin from the live site because it didn’t work but I did some debugging on a local installation and found the source of the problem.

    The site uses ACF and one of the custom fields contains a shortcode to a wpcf7 contact form.

    The custom theme called get_fields() from ACF before wp_head(). The call resolved the shortcode and called wpcf7_contact_form_tag_func that eventually called iqfix_wpcf7_recaptcha_form_tag_handler callback before iqfix_wpcf7_recaptcha_enqueue_scripts registered the ‘google-recaptcha’ script (too early).

    Therfore, it ended up in this if statement:

    if ( ! wp_script_is( ‘google-recaptcha’, ‘registered’ ) && function_exists( ‘wpcf7_recaptcha_enqueue_scripts’ ) ) {
    wpcf7_recaptcha_enqueue_scripts();
    }

    it evaluated true because of the above-mentioned situation and called the original wpcf7_recaptcha_enqueue_scripts() function which always loads v3.

    I don’t know if it is good to always call the original wpcf7_recaptcha_enqueue_scripts from wpcf7 (loads v3) in this if statement instead of checking which one (v2 or v3) in use and calling accordingly either wpcf7_recaptcha_enqueue_scripts or iqfix_wpcf7_recaptcha_enqueue_scripts.

    Please note, I haven’t reviewed the whole wpcf7 codebase so this is just a guess.

    Anyways, I think on average this if statement never evaluates to true because iqfix_wpcf7_recaptcha_enqueue_scripts registered the script beforehand.

    I fixed my custom theme so it calls get_fields() after wp_head() and everything works well.

    • This reply was modified 4 years ago by txtw.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Contact Form 7 5.4 tries to load rCaptcha V3 instead of V2’ is closed to new replies.