• Resolved Guy

    (@guyrutenberg)


    Hi,
    I’m trying to integrate captcha into Jetpack contact form. I can use the jetpack_contact_form_is_spam filter to mark the form submission as spam if the user fails to solve the captcha. The problem is that the user is not aware of failing to solve the captcha. Is there a better hook where I can check if the captcha is correct and alert the user in case it isn’t?

    Thanks,
    Guy

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Guy,

    When the captcha validation fails, you can return a WP_Error on the jetpack_contact_form_is_spam filter to abort the process submission and modify the contact form HTML. Example:

    add_filter( 'jetpack_contact_form_is_spam', function ( $is_spam, $submission ) {
    $is_captcha_valid = false; // Replace by calling your custom validation.

    if ( ! $is_captcha_valid ) {
    // My custom error.
    $error = new \WP_Error( 'captcha_failed', __( 'CAPTCHA verification failed. Please try again.') );

    // Modify the contact form HTML.
    add_filter( 'jetpack_contact_form_html', function ( $format_html) use ( $error ) {
    return '<pre>' . $error->get_error_message() . '</pre>' . $format_html;
    } );

    // Return a custom error to abort the form process submission.
    return $error;
    }

    return false;
    }, 10, 2 );

    Please let me know if it works.

    Thread Starter Guy

    (@guyrutenberg)

    Thank you, it works.

    Plugin Contributor Stef (a11n)

    (@erania-pinnera)

    Hey @guyrutenberg,

    That is fantastic news! We’re glad to hear you’re all set!

    If you’re finding Jetpack valuable and are happy with our support, please consider?leaving us a review ??

    I’m going to mark this thread as solved. If you have any further questions or need more help, you’re welcome to open another thread here. Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.