How to Add Google reCAPTCHA to Magento 2

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 spams 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:

  1. Add script to any head or where you want to use google recaptcha
  2. Add captcha code to phtml or block/page where you want to add v2 google recaptcha in the form
  3. If the captcha does not validate then add below code :
    In any form add onSubmit() event like :

That was all about Google reCAPTCHA v2 with Magento 2.

For a similar solution in Magento 1, check here.

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 🛡️

CTA_M2-Google-Invisible-recaptcha

5
(based on 3 Reviews)

Sanjay is a co-founder at Meetanshi. He is a certified Magento developer who loves creating Magento E-commerce solutions. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a Reply