How to Add Google reCAPTCHA to WordPress comments without plugin

| |

Hello Guys, Today in the tutorial, we will learn how to add Google reCAPTCHA to WordPress comments without plugin. If your blog also gets spam comments and through this, your spam score is increased and you are upset due to this, then you will not get any trouble because today I am going to tell you a solution through which this problem can be solved. Its solution is that we add Google reCAPTCHA to WordPress comments without plugin in your blog.

So let’s start

What is Google reCAPTCHA?

reCAPTCHA is a free anti-spam service provided by Google. It is easy to do it, it is also effective and at the same time it is even easier to implement it. It is best done to him for the API of the captcha. If you want to know more about it, then click on this link

This method has been used on the latest version of WordPress and has also been used in many themes such as GeneratePress, Twenty Seventeen theme, etc.

Steps to add Google reCAPTCHA to WordPress comments.

Step 1: Go to and register your website.

Add Google reCAPTCHA to WordPress comments without plugin

Step 2: View the “Site key” and “Secret key”  which will be used later in the code.

Step 3: Edit single.php from your theme folder (in my example /wp-content/themes/generatepress) and add the following code before get_header();

wp_enqueue_script('google-recaptcha', '');

Step 4: Edit functions.php, add the code from below with your site_key (line 5) and secret_key (line 15).

 * 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()) {

 * 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("" , false , $captcha_context), true);
if ($captcha_response['success'])
    return true;
    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');

No step five, that’s it!

I hope this solution or method will help you to get the desired result.

If you have any feedback/comments, please feel to comment or contact us.

Share on:

How to remove URL from Author Name in GeneratePress Theme?

Vishal Kumawat is the Author & Founder of the He has also completed his graduation in Bachelor of Computer Applications. He is a part time blogger and full time developer.

Leave a Comment