The post How to Add reCaptcha to WordPress Comments to Prevent Spam Comments appeared first on PluginsForWP.
]]>For months we received hundreds of spam comments every day. Luckily, we solve the problem by adding a reCaptcha box to the comments form.
This article will teach you how to add reCaptcha to your WordPress comment form to prevent spam messages.
reCaptcha is the box added to the form above the submit button and asking the user to prove that they are not a robot.
They are widespread and come in different verification options:
The most used one is the ‘I’m not a robot‘ checkmark, just like this one (that’s the one we use).

On the other hand, another popular one is proving that you are not a robot by selecting specific images from the same group as cars.

reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep malicious software from engaging in abusive activities on your website. Meanwhile, legitimate users will be able to login, make purchases, view pages, or create accounts and fake users will be blocked.
Official Google reCaptcha website
There are two versions of Google reCaptcha.
reCaptcha v2 – the box that appears inside the form (comments, login, register, etc.). They are the examples I showed you above, which is the version we will use in this tutorial.
The user will need to solve a challenge before submitting the form.
reCaptcha v3 – added to every page (or chosen pages) on your website and will analyze your behavior as a visitor. Human users act differently from bots (dah!!) and, therefore, be legit. It will be blocked if a robot tries to leave a comment or review.

We only want to add a reCaptcha box to the WordPress comments form specifically instead of all pages. Therefore, we will use the reCaptcha v2 type.
As mentioned above, spam comments are not fun and can damage your website or admin’s experience.
Furthermore, spam comments will take space on your database and, if not deleted frequently, will clog and slow it down.
Also, if you get an email notification (like we do) for every comment left on your website, it will pile up quickly. That will slow down your workflow and keep you from focusing on critical emails.

We often skipped valuable comments left by legit users because we assumed they were spam comments. It doesn’t happen anymore after adding the reCaptcha form to our website.
Before we added the reCaptcha feature, we had two options, constantly delete all the comments or turn off WordPress comments altogether, and we didn’t want to do that.
After enabling the reCaptcha option, all comments are legit and approved when receiving comments.
We can add reCaptcha in two different ways. The first way is by installing a plugin, and the second way is by using a PHP function. In this tutorial, we will cover both methods.
Regardless of your option, we will first need to generate our reCaptcha API keys.
Open the Google reCaptcha website and click the v3 Admin Console link to generate the keys.

Then, click on reCaptcha v2 and select the ‘I’m not a robot’ checkbox option. Enter your website’s domain name, accept the terms of service and click submit.

You’ll have both the public and secret API keys on the next screen. Keep this window open, and we will need to copy them shortly.

We can now move on to the second step and use the keys with a plugin or a function.
The first way to add reCaptcha to your website is by using a plugin. First, from inside your WordPress dashboard, navigate to Plugins -> Add New, and install the reCaptcha by BestWebSoft plugin.

Once activated, navigate to reCaptcha -> Settings, choose Version 2 and enter your site and secret keys generated on the last step.
Then, choose the forms you would like to enable reCaptcha for and scroll to the bottom of the page to save the changes.

Once saved, visit one of your blog posts, scroll to the comments section and see the added reCaptcha box.

Follow the section below if you want to use a function instead of a plugin.
We will use a PHP function to integrate reCaptcha with the comments form with this method.
First, navigate to Appearance -> Theme Editor and open the single.php (or singular.php) file of your child theme. Alternatively, you can also access the files by creating an FTP account.
If you don’t have a child theme yet, create one after reading how to make a WordPress child theme article.
Tip: please backup your website before editing core files.
Then, copy the code below and paste it just before the get_header(); function in the single.php file and click save.
wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');

Once saved, open the functions.php file, scroll to the bottom of it, and paste the code below:
/* Add Google recaptcha to WordPress comment box */
function add_google_recaptcha($submit_field) {
$submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="enter_your_site_key_here"></div><br>' . $submit_field['submit_field'];
return $submit_field;
}
if (!is_user_logged_in()) {
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' => 'enter_your_secret_key_here',
'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>"));
}
if (!is_user_logged_in()) {
add_action('pre_comment_on_post', 'verify_google_recaptcha');
}
Make sure to replace ‘enter_your_site_key_here‘ and ‘enter_your_secret_key_here‘ with the actual keys, we generated earlier. Once replaced, click save.

Finally, visit the comments section on one of your blog posts and verify that the reCaptcha box was added successfully. Go ahead and test the box by leaving a comment.
In this article, you learned how to add a reCaptcha box to WordPress comments in two different ways.
Leave us a comment and tell us which one of the methods you chose to perform this task.
The post How to Add reCaptcha to WordPress Comments to Prevent Spam Comments appeared first on PluginsForWP.
]]>The post How To Add reCaptcha To Contact Form 7 (v3) appeared first on PluginsForWP.
]]>Contact Form 7 is one of the most popular form plugins for WordPress used by millions around the world.
In this article, you’ll learn how to integrate reCaptcha with Contact Form 7 on your WordPress website.
Bots and malicious software uses websites’ forms to send messages or leave comments with abusive content or spam.
Often, the left content will damage your domain score, hurt the visitors’ experience, and decrease the overall quality of your website.
reCaptcha is a free service created by Google to help protect your website against that.
The service adds a test box that is very easy for humans to solve but very hard for bots.

When adding a reCaptcha to a contact form, you minimize the number of spam messages and guarantee a better user experience.
To integrate between reCaptcha and CF7 you’ll first need to install the plugin, then get the unique keys for your domain name, and finally, connect between the two.
From your WordPress dashboard, navigate to Plugins -> Add New. Then, search and install the Contact Form 7 plugin.

Once you activated the plugin, move forward and get the API keys.
To start using this tool, you’ll need to have a Google account and get the API keys for your domain name.
Open the official reCaptcha website and click on the Admin Console.

Fill the form with your information such as label, type, domain, and etc, and click submit.

Copy both site and secret keys from the next page.

After getting both keys, we can go back to our WordPress website.
In this step, we will paste the keys inside the CF7 integration screen.
First, navigate to Contact -> Integration.
Then, click on setup integration and enter the keys you generated in the last step.

Once pasted, you will see the message that reCaptcha is active on your site.

In the last step, we need to verify that we successfully added the reCaptcha box to our website.
After entering the reCaptcha keys into the Contact Form 7 settings page, we need to verify that it’s working and active on our website.
Thus, visit your website or the page you’re displaying the contact form 7 on and look for the reCaptcha logo at the bottom right corner.

If you see the logo, it means that you connected between the two successfully and your form is now secured.
That’s it, the two are now integrated together and you won’t get any spam messages any longer.
In this article, you learned how to integrate Google reCaptcha with Contact Form 7 to secure your forms.
Leave us a comment and let us know if you have any questions about the process.
The post How To Add reCaptcha To Contact Form 7 (v3) appeared first on PluginsForWP.
]]>