Google reCAPTCHA is becoming popular since from the day it’s announced. The performance of this Google services is not been evaluated yet its more than just a website spam protection.Add Google reCAPTCHA

What is Google Recaptcha?

Google reCAPTCHA is a free service that protects your website from spam and abuse. reCAPTCHA uses an advanced risk analysis engine and adaptive CAPTCHAs to keep automated software from engaging in abusive activities on your site. It does this while letting your valid users pass through with ease.

reCAPTCHA compromises more than just spam protection. Every time CAPTCHAs are solved, that human effort helps digitize text, annotate images, and build machine learning datasets to improve services. This in turn helps preserve books, improve maps, and solve hard AI problems.

Advanced Security

Google reCAPTCHA is built for security. Armed with state of the art technology, it always stays at the forefront of spam and abuse fighting trends. reCAPTCHA is on guard for you, so you can rest easy from unusual spam activities.

reCAPTCHA inputs are the frustrating experience on the web. They’re painful enough for majority of users. However, CAPTCHAs are absolutely vital in fight against spam and abuse. It is the only logic therefore that Google Developers came up with the best CAPTCHA solution so far, toward end of 2014.

In this tutorial I am going to explain how to add Google reCAPTCHA in WordPress Comments to stop bot comments. I have implemented the same in my blog.

Let’s add Google reCAPTCHA to WordPress Comments and see how it works.

Note: Tested on WordPress 4.9.6 with Twenty Seventeen theme and Newspaper Theme.

  1. Register your website https://www.google.com/recaptcha/admin#list  to Google  reCAPTCHA Register on Google reCaptcha
  2. View the “Site key” and “Secret key” which will be used in the code.
  3. Paste this <script src='https://www.google.com/recaptcha/api.js'></script>snippet before the closing </head> tag on your website.
  4. Edit single.php file from your theme folder i.e (wp-content/themes/newspaper) and add the following code before get_header();
    wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
  5. Edit functions.php, add the following code from below with your site_key (line 5) and secret_key (line 15).
    /**
     * Google recaptcha add before the submit button
     */
    function add_google_recaptcha($submit_field) {
        $submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="Your Site Key"></div><br>' . $submit_field['submit_field'];
        return $submit_field;
    }
    add_filter('comment_form_defaults','add_google_recaptcha');
     
    /**
     * Google recaptcha check, validate and catch the spammer
     */
    function is_valid_captcha($captcha) {
    $captcha_postdata = http_build_query(array(
                                'secret' => 'Your Secret Key',
                                'response' => $captcha,
                                'remoteip' => $_SERVER['REMOTE_ADDR']));
    $captcha_opts = array('http' => array(
                          'method'  => 'POST',
                          'header'  => 'Content-type: application/x-www-form-urlencoded',
                          'content' => $captcha_postdata));
    $captcha_context  = stream_context_create($captcha_opts);
    $captcha_response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify" , false , $captcha_context), true);
    if ($captcha_response['success'])
        return true;
    else
        return false;
    }
     
    function verify_google_recaptcha() {
    $recaptcha = $_POST['g-recaptcha-response'];
    if (empty($recaptcha))
        wp_die( __("<b>ERROR:</b> please select <b>I'm not a robot!</b><p><a href='javascript:history.back()'>« Back</a></p>"));
    else if (!is_valid_captcha($recaptcha))
        wp_die( __("<b>Go away SPAMMER!</b>"));
    }
    add_action('pre_comment_on_post', 'verify_google_recaptcha');

    Add Google Recaptcha in WordPress Comments

Conclusion

Google reCAPTCHA is really awesome spam protection service by Google. Give it a try. If you are not a coder then, there are some WordPress plugin as well which let you add Google captcha easily. Do a Google and find it out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here