Enable reCAPTCHA on WooCommerce Reviews

And END spam on your Product reviews

ยท

6 min read

Online stores have become an indispensable part of our daily lives. The convenience, variety, and ease of shopping from the comfort of our homes have contributed to the rapid growth of eCommerce platforms, WooCommerce being one of the most popular ones. As a leading eCommerce solution, WooCommerce offers businesses a solid foundation to build their online presence. One of its essential features is the ability to collect product reviews from customers, which plays a significant role in influencing the purchase decisions of other potential buyers.

However, the potential for spam and fake reviews can damage the credibility of an online store. It is critical to ensure that the reviews section remains a trusted source of information for customers. One way to enhance the security of WooCommerce reviews is by integrating Google reCAPTCHA, a powerful and widely-used security solution that distinguishes between genuine users and automated bots. In this blog post, we will walk you through the process of adding Google reCAPTCHA to your WooCommerce reviews, helping you maintain the integrity of your online store and protect it from spammers and bots.

Google reCAPTCHA, developed by Google, is a free service that verifies if an interaction is performed by a human or an automated bot. The service comes in multiple versions, including reCAPTCHA v2 and reCAPTCHA v3. The former requires users to click on an "I'm not a robot" checkbox, while the latter runs in the background, without any user interaction, and assigns a score to each request, determining the likelihood of it being a bot. Regardless of the version you choose, integrating Google reCAPTCHA with your WooCommerce store can significantly reduce the chances of spam and fake reviews.

Before diving into the step-by-step guide, let's briefly discuss the prerequisites for this tutorial. You will need to have a WordPress website with WooCommerce installed and a basic understanding of WordPress plugins. Additionally, you will need to sign up for a Google reCAPTCHA API key pair, which involves registering your site with Google and selecting the desired reCAPTCHA version (v2 or v3). Once you have obtained the Site Key and Secret Key, you are ready to proceed with the integration process.

The integration process involves creating a custom WordPress plugin to enable Google reCAPTCHA for WooCommerce reviews. This approach ensures that your reCAPTCHA integration remains intact even if you update or change your theme. The custom plugin will consist of a PHP file that you will create in the wp-content/plugins directory of your WordPress installation. In this file, you will add code snippets that will enqueue the necessary reCAPTCHA scripts, display the reCAPTCHA widget in the review form, and verify the reCAPTCHA response before submitting the review. This guide provides you with the complete code for both reCAPTCHA v2 and v3, which you can easily copy, modify, and use according to your needs.

After creating and activating the custom plugin, Google reCAPTCHA will be enabled for your WooCommerce reviews. This additional layer of security will help prevent spam and fake reviews, ensuring that your online store remains a trusted source of information for potential customers. Moreover, by safeguarding the integrity of your reviews, you can improve user experience and potentially increase conversions.

In conclusion, integrating Google reCAPTCHA with WooCommerce reviews is an effective way to protect your online store from spammers and bots. With this comprehensive guide, you can easily implement this security solution and ensure the credibility of your reviews section. Stay ahead of malicious actors and provide your customers with a secure and reliable online shopping experience by adding Google reCAPTCHA to your WooCommerce reviews today.

To enable Google reCAPTCHA on WooCommerce reviews, follow these steps:

  1. Sign up for a Google reCAPTCHA API key pair by visiting https://www.google.com/recaptcha.

  2. Choose the reCAPTCHA version (v2 or v3) and register your site.

  3. Note down your Site Key and Secret Key.

Now, create a custom WordPress plugin to enable Google reCAPTCHA on WooCommerce reviews:

  1. Create a new folder in your wp-content/plugins directory, e.g., woocommerce-recaptcha-review.

  2. Inside this folder, create a new file named woocommerce-recaptcha-review.php.

  3. Open the file in a text editor and add the following code:

<?php
/**
 * Plugin Name: WooCommerce reCAPTCHA Review
 * Description: Adds Google reCAPTCHA to WooCommerce product reviews
 * Version: 1.0
 * Author: Your Name
 * Author URI: Your Website
 * License: GPL2
 */

// Replace with your own keys
define('GOOGLE_RECAPTCHA_SITE_KEY', 'your_site_key');
define('GOOGLE_RECAPTCHA_SECRET_KEY', 'your_secret_key');

// Enqueue reCAPTCHA scripts
function wcr_enqueue_recaptcha_scripts() {
    wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'wcr_enqueue_recaptcha_scripts');

// Add reCAPTCHA to the review form
function wcr_add_recaptcha_to_review_form() {
    echo '<div class="g-recaptcha" data-sitekey="' . GOOGLE_RECAPTCHA_SITE_KEY . '"></div>';
}
add_action('comment_form_after_fields', 'wcr_add_recaptcha_to_review_form');

// Verify reCAPTCHA response
function wcr_verify_recaptcha($commentdata) {
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = sanitize_text_field($_POST['g-recaptcha-response']);

        $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
            'body' => array(
                'secret' => GOOGLE_RECAPTCHA_SECRET_KEY,
                'response' => $recaptcha_response,
                'remoteip' => $_SERVER['REMOTE_ADDR']
            )
        ));

        if (!is_wp_error($response) && isset($response['body'])) {
            $result = json_decode($response['body'], true);
            if (!$result['success']) {
                wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
            }
        } else {
            wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
        }
    } else {
        wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
    }

    return $commentdata;
}
add_filter('preprocess_comment', 'wcr_verify_recaptcha', 10, 1);
  1. Replace 'your_site_key' and 'your_secret_key' with the Site Key and Secret Key obtained from Google reCAPTCHA.

  2. Save the file and activate the plugin in your WordPress dashboard under "Plugins".

  3. The Google reCAPTCHA should now be enabled for WooCommerce reviews.

Note: This code snippet assumes you are using reCAPTCHA v2. If you use reCAPTCHA v3, you will need to modify the code snippet accordingly. Below is the modified code snippet for reCAPTCHA v3:

<?php
/**
 * Plugin Name: WooCommerce reCAPTCHA Review
 * Description: Adds Google reCAPTCHA v3 to WooCommerce product reviews
 * Version: 1.0
 * Author: Your Name
 * Author URI: Your Website
 * License: GPL2
 */

// Replace with your own keys
define('GOOGLE_RECAPTCHA_SITE_KEY', 'your_site_key');
define('GOOGLE_RECAPTCHA_SECRET_KEY', 'your_secret_key');

// Enqueue reCAPTCHA scripts
function wcr_enqueue_recaptcha_scripts() {
    wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . GOOGLE_RECAPTCHA_SITE_KEY, array(), null, true);
    wp_add_inline_script('google-recaptcha', '
        jQuery(document).ready(function() {
            jQuery("#commentform").on("submit", function(e) {
                e.preventDefault();
                grecaptcha.ready(function() {
                    grecaptcha.execute("' . GOOGLE_RECAPTCHA_SITE_KEY . '", {action: "submit_review"}).then(function(token) {
                        jQuery("#commentform").prepend("<input type=\'hidden\' name=\'g-recaptcha-response\' value=\'" + token + "\'>");
                        jQuery("#commentform").off("submit").submit();
                    });
                });
            });
        });
    ');
}
add_action('wp_enqueue_scripts', 'wcr_enqueue_recaptcha_scripts');

// Verify reCAPTCHA response
function wcr_verify_recaptcha($commentdata) {
    if (isset($_POST['g-recaptcha-response'])) {
        $recaptcha_response = sanitize_text_field($_POST['g-recaptcha-response']);

        $response = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', array(
            'body' => array(
                'secret' => GOOGLE_RECAPTCHA_SECRET_KEY,
                'response' => $recaptcha_response,
                'remoteip' => $_SERVER['REMOTE_ADDR']
            )
        ));

        if (!is_wp_error($response) && isset($response['body'])) {
            $result = json_decode($response['body'], true);
            if ($result['success'] && $result['action'] == 'submit_review' && $result['score'] >= 0.5) {
                return $commentdata;
            } else {
                wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
            }
        } else {
            wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
        }
    } else {
        wp_die(__('reCAPTCHA verification failed. Please try again.', 'woocommerce-recaptcha-review'), __('reCAPTCHA Verification', 'woocommerce-recaptcha-review'), array('back_link' => true));
    }
}
add_filter('preprocess_comment', 'wcr_verify_recaptcha', 10, 1);
  1. Replace 'your_site_key' and 'your_secret_key' with the Site Key and Secret Key obtained from Google reCAPTCHA.

  2. Save the file and activate the plugin in your WordPress dashboard under "Plugins".

  3. The Google reCAPTCHA v3 should now be enabled for WooCommerce reviews.

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!

ย