Last updated on September 24, 2023

Add a Google reCAPTCHA to the Comment Form

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Protect against spam with reCAPTCHA.

To add a Google reCAPTCHA to the comment form in WordPress, you can use the comment_form hook along with the wpsnippets_add_recaptcha_to_comment_form function. This code snippet will display the reCAPTCHA widget below the comment textarea.

function wpsnippets_add_recaptcha_to_comment_form() {
    if ( ! is_user_logged_in() ) {
        $site_key = 'YOUR_RECAPTCHA_SITE_KEY';
        ?>
        <div class="g-recaptcha" data-sitekey="<?php echo esc_attr( $site_key ); ?>"></div>
        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
        <?php
    }
}
add_action( 'comment_form', 'wpsnippets_add_recaptcha_to_comment_form' );

Replace 'YOUR_RECAPTCHA_SITE_KEY' with your actual reCAPTCHA site key. You can obtain the site key by creating a reCAPTCHA API key pair from the Google reCAPTCHA website.

Remember to enqueue the reCAPTCHA script using the wp_enqueue_script function in your theme’s functions.php file or a custom plugin.

This code snippet is useful when you want to add an extra layer of spam protection to the comment form in WordPress by integrating Google reCAPTCHA. It ensures that only human users can submit comments by verifying their interaction with the reCAPTCHA widget.

Examples

Example 1: Adding Google reCAPTCHA to the Comment Form using a Plugin

This example demonstrates how to add Google reCAPTCHA to the comment form using a WordPress plugin called “reCAPTCHA Integration for WordPress”.

// Add the reCAPTCHA field to the comment form
function wpsnippets_add_recaptcha_to_comment_form() {
    if ( function_exists( 'gglcptch_comment_form' ) ) {
        gglcptch_comment_form();
    }
}
add_action( 'comment_form_after_fields', 'wpsnippets_add_recaptcha_to_comment_form' );

In this example, we use the comment_form_after_fields action hook to add the reCAPTCHA field to the comment form. The gglcptch_comment_form() function is provided by the “reCAPTCHA Integration for WordPress” plugin and generates the necessary HTML markup for the reCAPTCHA field.

Example 2: Adding Google reCAPTCHA to the Comment Form using Custom Code

This example demonstrates how to add Google reCAPTCHA to the comment form using custom code without relying on a plugin.

// Add the reCAPTCHA field to the comment form
function wpsnippets_add_recaptcha_to_comment_form() {
    if ( function_exists( 'gglcptch_display' ) ) {
        echo gglcptch_display( array( 'echo' => false ) );
    }
}
add_action( 'comment_form_after_fields', 'wpsnippets_add_recaptcha_to_comment_form' );

In this example, we use the comment_form_after_fields action hook to add the reCAPTCHA field to the comment form. The gglcptch_display() function is provided by the reCAPTCHA library and generates the necessary HTML markup for the reCAPTCHA field. We use the echo parameter set to false to retrieve the HTML markup as a string and then echo it manually.

Example 3: Verifying Google reCAPTCHA on Comment Submission

This example demonstrates how to verify the Google reCAPTCHA response when a comment is submitted.

// Verify the reCAPTCHA response on comment submission
function wpsnippets_verify_recaptcha_on_comment_submission( $commentdata ) {
    if ( function_exists( 'gglcptch_check_answer' ) ) {
        $result = gglcptch_check_answer();
        if ( ! $result ) {
            wp_die( 'reCAPTCHA verification failed. Please try again.' );
        }
    }
    return $commentdata;
}
add_filter( 'preprocess_comment', 'wpsnippets_verify_recaptcha_on_comment_submission' );

In this example, we use the preprocess_comment filter to verify the reCAPTCHA response before the comment is saved. The gglcptch_check_answer() function is provided by the reCAPTCHA library and checks if the user’s response is valid. If the verification fails, we use wp_die() to display an error message and prevent the comment from being saved.

Last updated on September 24, 2023. Originally posted on September 22, 2023.

Leave a Reply

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