How to Add Google reCAPTCHA to Magento 2
According to BuiltWith, there are 52,912 live Magento 2 stores. Ideal prey for the hackers!
A compromised store with a host to spam would lead to customers being skeptical to buy. Hesitated customers mean a breakdown in E-commerce potential. Insecure Magento 2 platform will cost the business a loss in revenue as well as customers’ trust.
Thankfully, Google offers an easy way to secure the store. With Google’s reCAPTCHA v2, differentiate between bots and humans.
Add Google reCAPTCHA to Magento 2 and enable the “I’m not a robot” checkbox. A user has to tick the checkbox indicating they are not robots but humans. Either they can go through without any challenge or have to validate their identity as a human.
However, to integrate the Google reCAPTCHA v2 with Magento 2, you require to follow the below method.
Implement this method to secure your store when a user tries to use the contact form, subscribe form, etc.
Note:
- Prior to implementing the below method, sign up for the public and secret key pair specifically for the store’s domain.
- Make sure the reCAPTCHA authentication works for the subdomains as well. If not, the reCAPTCHA “I’m not a robot” box will display but with an error.
- Check the details at Google reCAPTCHA documentation.
Method to Add Google reCAPTCHA to Magento 2:
- Add script to any head or where you want to use google Recaptcha
1<script src="https://www.google.com/recaptcha/api.js" type="text/javascript" xml="space"></script> - Add captcha code to phtml or block/page where you want to add v2 google Recaptcha in the form
12345678<div class="form-group required"> //change class as per themes<div class="g-recaptcha required" id="recaptcha"data-sitekey="your-site-key"></div><div style="display:none" id="error-msg"><span><?php echo __('This is a required field.') ?></span></div></div> - If the captcha does not validate then add the below code:
12345678910111213<script>function checkCaptcha() {if ((jQuery('#g-recaptcha-response').val()) === '') {jQuery('#error-msg').css('display', 'block');jQuery('#error-msg').css('color', '#df280a');jQuery('#error-msg').css('font-size', 13);return false;}else {jQuery('#error-msg').css('display', 'none');}}</script>
12<form class="form contact" action="<?php echo $block->getFormAction(); ?>" id="contact-form" method="post" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}' onSubmit="return checkCaptcha() ;"></form>
That was all about Google reCAPTCHA v2 with Magento 2.
For a similar solution in Magento 1, check Google reCAPTCHA to Magento.
In case if you’re facing any issue on the frontend, read the solution to reCAPTCHA Not Working in Chrome, Firefox, or Any BrowserreCAPTCHA Not Working in Chrome, Firefox, or Any Browser
I’d be happy to hear your opinions in the Comments section below about the use of the reCAPTCHA and how your customers welcomed it.
Keep Secure 🛡️
Protect your online store from spam bots and automated abuses using Magento 2 Google reCAPTCHA v3 extension.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Where is your the Secrety key option ?
Hey Zuby, There is no need of a Secret Key in Google reCAPTCHA v2,
I suggest You to refer below given documentation :
https://developers.google.com/recaptcha/docs/display
Love all of your tutorials mate, been a massive help!
Hello Callum,
That is great to know that our content is helpful for you.
Thanks for appreciation.
Thank You
Hi Sanjay,
How to use google recapcha in custom payment gateway in checkout page(Magento2)
I followd your steps but not working captcha. Can you please help me.
Thanks
Hello Soundar,
Form submit action might not be there on button click event.
So, first you need to modify the code on the order placed click event.
Thank You
follow the same step, pasted site key also(Used visible captcha –I m not a robot)…
but recaptcha not visible in custom form .????
Hello Priya,
We’ve verified the above code and it’s working from our end.
Please recheck your implemented solution.
Thank You