Adding reCAPTCHA to a WordPress website’s comments form using a function effectively enhances security and reduces spam submissions. WordPress, one of the most popular content management systems, attracts considerable attention from spammers who attempt to exploit its comment forms. By implementing reCAPTCHA, a widely used and trusted solution provided by Google, website owners can protect their websites from unwanted spam comments.
In this guide, we will explore the process of integrating reCAPTCHA into a WordPress website’s comments form using a function. We will discuss the necessary steps, from generating reCAPTCHA API keys to modifying the website’s code. This method gives you greater control over the implementation, ensuring that the reCAPTCHA functionality is seamlessly integrated into your website’s comment section.
By following this tutorial, you will be able to safeguard your WordPress website’s comments section against automated spam bots, while providing a better user experience for genuine visitors who wish to engage in meaningful discussions. Let’s dive into the process of adding reCAPTCHA to your WordPress website’s comments form using a function and enjoy the benefits of a spam-free comment section.
Adding reCAPTCHA Using Function
With this solution, we will use a PHP function that integrates reCaptcha with the WordPress comments form.
Goto => Appearance -> Theme Editor and open the Single.php Or Singular.php File of your WordPress Child Theme. If you don’t have a child theme then Create One By Reading This Article.
Note: Always Create a Backup of Your Website before adding any Custom Functions to your website.
Now, copy the below code and paste it just before the get_header(); function in the Single.php or Singular.php file and “Click Save Button”.
wp_enqueue_script(‘google-recaptcha’, ‘https://www.google.com/recaptcha/api.js‘);
Now, open function.php file, scroll down to it, and paste the below code:
/* 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’);
}
Now, replace the site key with your Site Key and the secret key with your Secret Key. Once you replace the site key and secret key then “Click The Save Button”.
visit the comments section on one of your blog posts and verify that the reCaptcha box was added successfully.
Post a Comment