{"id":494,"date":"2019-07-04T22:35:20","date_gmt":"2019-07-04T22:35:20","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/07\/04\/add-google-recaptcha-to-magento\/"},"modified":"2025-05-22T16:27:06","modified_gmt":"2025-05-22T10:57:06","slug":"add-google-recaptcha-to-magento","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-google-recaptcha-to-magento\/","title":{"rendered":"How to Add Google reCAPTCHA to Magento"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Previously, you read how to add&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-google-recaptcha-to-magento-2\/\">Google reCAPTCHA to Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I\u2019ve come up with a similar solution for Magento 1.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Captcha, i.e.,&nbsp;<strong>C<\/strong>ompletely&nbsp;<strong>A<\/strong>utomated&nbsp;<strong>P<\/strong>ublic&nbsp;<strong>T<\/strong>uring test to tell&nbsp;<strong>C<\/strong>omputers and&nbsp;<strong>H<\/strong>umans&nbsp;<strong>A<\/strong>part, is a service by Google that prevents the store from spam and bots. Implementing&nbsp;<a href=\"https:\/\/developers.google.com\/recaptcha\/docs\/versions#checkbox\" target=\"_blank\" rel=\"noreferrer noopener\">Google reCAPTCHA v2<\/a>&nbsp;helps to identify if a visitor in your site is a human or a bot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As Magento, by default, does not offer a reliable solution for security against spams, merchants have to rely on 3rd party solutions and the best choice is Google-developed anti-spam reCAPTCHA that displays the &nbsp;\u201cI\u2019m not a robot\u201d checkbox.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To&nbsp;<em><strong>add Google reCAPTCHA to Magento<\/strong><\/em>, you need to follow the given method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Additionally, check these steps prior to implementing the programmatic method:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Register for the public and secret key pair Magento store\u2019s domain.<\/li>\n\n\n\n<li>Ensure if the reCAPTCHA authentication works for the subdomains as well to avoid any error.<\/li>\n\n\n\n<li>Refer to&nbsp;<a href=\"https:\/\/developers.google.com\/recaptcha\/docs\/display\" target=\"_blank\" rel=\"noreferrer noopener\">Google reCAPTCHA documentation<\/a>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Google reCAPTCHA to Magento :<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Add the below script to any head or where you want to use&nbsp;google recaptcha<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script src=\"https:\/\/www.google.com\/recaptcha\/api.js\" type=\"text\/javascript\" xml=\"space\"&gt;&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Add captcha code to phtml or block\/page where you want to add v2&nbsp; google recaptcha in the form<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u00a0&lt;div class=\"form-group required\"> \u00a0 \/\/change class as per themes\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;div class=\"g-recaptcha required\" id=\"recaptcha\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0data-sitekey=\"your-site-key\">\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/div>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;div style=\"display:none\" id=\"error-msg\">\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;span>&lt;?php echo __('This is a required field.') ?>&lt;\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&lt;\/div>\n\u00a0 \u00a0&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. If the captcha does not validate then add below code<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> &lt;script>\n\u00a0 \u00a0 function checkCaptcha() {\n\u00a0 \u00a0 \u00a0 \u00a0 if ((jQuery('#g-recaptcha-response').val()) === '') {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 jQuery('#error-msg').css('display', 'block');\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 jQuery('#error-msg').css('color', '#df280a');\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 jQuery('#error-msg').css('font-size', 13);\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return false;\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 \u00a0 \u00a0 else {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 jQuery('#error-msg').css('display', 'none');\n\u00a0 \u00a0 \u00a0 \u00a0 }\n\u00a0 \u00a0 }\n\u00a0 \u00a0&lt;\/script><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In any form, add&nbsp;<em><strong>onSubmit<\/strong><\/em>&nbsp;event like :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u00a0 \u00a0&lt;form class=\"form contact\" action=\"&lt;?php echo $block->getFormAction(); ?>\" id=\"contact-form\" method=\"post\" data-hasrequired=\"&lt;?php echo __('* Required Fields') ?>\" data-mage-init='{\"validation\":{}}' onSubmit=\"return checkCaptcha() ;\"><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stay Secure  \ufe0f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously, you read how to add&nbsp;Google reCAPTCHA to Magento 2. Today, I\u2019ve come up with a similar solution for Magento 1. Captcha, i.e.,&nbsp;Completely&nbsp;Automated&nbsp;Public&nbsp;Turing test to&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-494","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=494"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":15354,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/494\/revisions\/15354"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}