How to Add reCAPTCHA to WordPress Comment Form

How to Add reCAPTCHA to WordPress Comment Form

Are you looking for ways to get rid of spam comments? Most of the spam comments are done by robots  We will use Recaptcha to get rid of the robots. We will show How to Add reCAPTCHA to the WordPress Comment Form.

The more popular you are, the more spam comments you will receive. It’s non-stop. So, it’s important to protect your comment form from robots. 

What is reCAPTCHA:

reCAPTCHA is a CAPTCHA system that enables web hosts to distinguish between human and automated access to websites. The original version asked users to decipher hard-to-read text or match images. Version 2 also asked users to decipher text or match images if the analysis of cookies and canvas rendering suggested the page was being downloaded automatically. Since version 3, reCAPTCHA will never interrupt users and is intended to run automatically when users load pages or click buttons. reCAPTCHA is owned by Google. (This information was collected from Wikipedia)

Registering Your Website to Get reCAPTCHA API Keys:

Register your domain into the google Recaptcha link to get API Keys. Go to this link to register for a new site.

Register your domain to get the API key

Choose reCAPTCHA type to reCAPTCHA v2 > “I’m not a robot” Checkbox. 

Fill out the form with your real information and click submit button.

If everything is okay. You will get success messages with the site key and secret key. Copy and save that two keys. 

Add reCAPTCHA to WordPress Comment Form without plugin

It seems that you already collect the site key and secret key. I showed you to add reCAPTCHA in the comment form with the plugin. If you want to use it you can use it.

But you only use two functions to add reCAPTCHA to the WordPress Comment Form.

Use this code on your function.php to add reCAPTCHA :

add_filter( 'comment_form_defaults', 'wpk_change_comment_form_defaults');
function wpk_change_comment_form_defaults( $default ) {
    $commenter = wp_get_current_commenter();
    $sitekey = 'ENTER YOUR SITEKEY';
    $default[ 'fields' ][ 'url' ] .= '<div style="margin-bottom: 15px" class="g-recaptcha" data-sitekey="'.$sitekey.'"></div>';
    return $default;
}

Note: Replace “ENTER YOUR SITEKEY” with your Sitkey which you collected from Google.

Use this code on your function.php to add reCAPTCHA verification:

add_filter( 'preprocess_comment', 'wpk_verify_comment_meta_data' );
function wpk_verify_comment_meta_data( $commentdata ) {
   if (isset($_POST['g-recaptcha-response'])) {
        $secretkey = 'ENTER YOUR SECRET KEY';
        $response = wp_remote_get( 'https://www.google.com/recaptcha/api/siteverify?secret='.$secretkey.'&response=' . $_POST['g-recaptcha-response'] );
        $response = json_decode($response['body'], true);
        if (true == $response['success']) {
            return $commentdata;
        }else {
            wp_die( __( 'Error: Prove You are Human.' ), '',array('back_link'=> true) );
        }
    }
}

Note: Replace “ENTER YOUR SECRET KEY” with your secret key which you collected from Google.

I hope You have added it properly.

Add reCAPTCHA to WordPress Comment Form with a plugin

Install Plugin for Google reCAPTCHA

To add google Recaptcha my favorite plugin is “Simple Google reCAPTCHA”. By using this plugin you can protect your –

  • Login form, 
  • Registration form
  • Reset password form
  • Comment form
  • New password form

Make your site protected from robot, spam, and brute force attacks.

Install and active Simple Google reCAPTCHA plugin

Add reCAPTCHA to WordPress Comment Form:

If you have installed the “Simple Google reCAPTCHA” plugin then you will see options under the setting menu named Settings >> “Google reCAPTCHA”.

Google reCAPTCHA form

You already have the site key and secret key. Fill out the form with the key and click Save Change. Once you save you will see reCAPTCHA on the supported form.

Congratulations, You have successfully Added reCAPTCHA to the WordPress Comment Form.

Note: Google reCAPTCHA verification will be required only for not logged-in users.

Conclusion:

Everyone who has a blog site or any type of site faces this problem. It’s annoying. Too many comments every day. If you use reCAPTCHA, Spam comments will decrease.

I hope you will like the article. If you face a problem please comment below.

See More:

How to change the default Gravatar for WordPress comments

One comment

  1. A person essentially help to make seriously articles I would state. This is the very first time I frequented your web page and thus far? I surprised with the research you made to create this particular publish extraordinary. Magnificent job!

Leave a Reply

Your email address will not be published. Required fields are marked *