Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How To Add Google reCAPTCHA To WordPress Comment Form Without Plugin

September 15, 2022
in Blog, Wordpress
2
ADVERTISEMENT

Table of Contents

Generate reCaptcha API Keys

1. Open the Google reCaptcha website and click on the v3 Admin Console link to get your Site key and Secret API Key.

Bg535
  • In the ‘Label’ section name it as you like. For example, you can name it My Site reCaptcha…
  • Then select the reCAPTCHA type, there are two versions reCaptcha v3 and reCaptcha v2. For Wordpress comment form, you should use reCaptcha v2.

  • Then in the Domain section enter your domain (you can add all your sites one by one).

  • Next, enter your email address and Accept the reCAPTCHA Terms of Service. Finally, click the “Submit” button.

Bg536

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.

Bg537

Add reCaptcha With a Function

After successfully registering your site to Google reCaptcha you will need to customize your theme in WordPress.

Note Before editing your theme files, please take a backup of your whole site, if anything goes wrong you can recover your current site using the backup.

1. Navigate to Appearance -> Theme Editor and open the single.php file of your theme or child theme.

2. Then, copy the below code then paste it just before the get_header(); function in the single.php file then save the change.

wp_enqueue_script('google-recaptcha', 'https://www.google.com/recaptcha/api.js');
Bg533

3. Once saved, open the functions.php file, scroll to the bottom of it, and paste the below code:

Note Don’t forget to replace line 5 and line 19 with the actual keys we generated earlier.
/**
Google recaptcha add before the submit button
 */
function add_google_recaptcha($submit_field) {
    $submit_field['submit_field'] = '<div class="g-recaptcha" data-sitekey="your_site_key"></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' => 'your_secret_key',
                            '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');
}

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.

Bg534

reCAPTCHA free one million assessments are per organization. The limit aggregates use across all accounts and all sites.

Bg600
5/5 - (1 vote)
Previous Post

How to Append Lines to /etc/hosts File in Linux using Shell Script without Open The hosts file with Text Editor

Next Post

How to Fix Cannot Start Microsoft Outlook and Cannot Open the Outlook Window

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Comments 2

  1. Gabi says:
    2 years ago

    It doesnt work for me, unfortunately.

    Reply
  2. Markus says:
    2 years ago

    Very good post! We are linking to this great article on our website.
    Keep up the good writing.

    Reply

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory