How to add reCAPTCHA 2 to WordPress site without a plugin?

3,478 views
Skip to first unread message

Ilona Thompson

unread,
Dec 4, 2014, 7:28:46 PM12/4/14
to reca...@googlegroups.com
How do I add the new reCAPTCHA (V2) to my site without a plugin? If I can't, what plugins are available that support the new version?

James Turner

unread,
Dec 5, 2014, 4:49:00 AM12/5/14
to reca...@googlegroups.com
Hi Ilona,

A library has been released at https://github.com/phpninjas/ReCaptcha 
It supports the validation of reCAPTCHA v2 , but you will have to write your own wordpress plugin for it.

Ilona Thompson

unread,
Dec 5, 2014, 6:39:21 PM12/5/14
to reca...@googlegroups.com
I don't know how to do that. Is there a way that I can copy and paste the site keys and the Javascript text into my stylesheet.css?

James Turner

unread,
Dec 5, 2014, 7:03:34 PM12/5/14
to reca...@googlegroups.com
Sadly it's not really as simple as that.

Yes you can get the client side captcha easily enough just by copying and pasting the code below into your html template (not the css file, css can't process javascript nor html).
Obviously replace YOUR_SITE_KEY with the site key provided by google. But this will not actually verify the integrity of the submitted request until you process the submitted information on the php side.

<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>


The difficult part comes with the server end (i.e. any php processing) because you need to verify that the submitted form contains a field called "
g-recaptcha-response" and you need to call google to validate this.
Because you're using wordpress you will likely have to intercept the request and verify with google. I would recommend a library and using composer, but I don't think you're that far advanced.

You will have to wait for someone to release a wordpress plugin, or simply write it yourself.

Ilona Thompson

unread,
Dec 5, 2014, 7:09:19 PM12/5/14
to reca...@googlegroups.com
Are there any plugins available for the new version of reCAPTCHA?

Zane

unread,
Dec 8, 2014, 1:12:39 PM12/8/14
to reca...@googlegroups.com
Hi IIona,

I'm currently implementing this in my plugin ZM AJAX Login & Register Pro, as the Google Docs say the front-end implementation is straight forward, but the server side can be troublesome, as it is custom.

As of know I've implemented it into the plugin, but having an issue with an AJAX request.

Below is a snippet of my PHP code if that helps, this code used when the form is submitted:

if ( empty( $_POST['g-recaptcha-response'] ) ){
    // User didn't check recaptcha, handle the error
} else {

    $params = http_build_query( array(
        'secret' => $secret,
        'response' => $_POST['g-recaptcha-response'],
        'remoteip'  => $some_ip
        ) );

    $response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?' . $params );

    $captcha_resposne = false;

    if ( 200 == $response['response']['code'] ) {
        $captcha_resposne = json_decode( $response['body'], true );
    
        // missing-input-secret    The secret parameter is missing.
        // invalid-input-secret    The secret parameter is invalid or malformed.
        // missing-input-response  The response parameter is missing.
        // invalid-input-response  The response parameter is invalid or malformed.
    }

    // If captcha respsonse is true we sign them on
    if ( $captcha_resposne['success'] === true ){
        // handle the success
    } else {
        // handle the failure
    }
}


Reply all
Reply to author
Forward
0 new messages