{"id":2264,"date":"2025-01-10T13:47:51","date_gmt":"2025-01-10T11:47:51","guid":{"rendered":"https:\/\/plumrocket.com\/learn\/?p=2264"},"modified":"2025-06-18T11:08:05","modified_gmt":"2025-06-18T08:08:05","slug":"how-to-add-google-recaptcha-to-custom-form%e2%80%8b-in-magento-2","status":"publish","type":"post","link":"https:\/\/plumrocket.com\/learn\/magento-2-add-google-recaptcha","title":{"rendered":"How to Add Google reCAPTCHA to Custom Form\u200b in Magento 2"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full disable_zoom\"><img loading=\"lazy\" width=\"1600\" height=\"600\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1.png\" alt=\"How to Add Google reCAPTCHA to Custom Form\u200b in Magento 2\" class=\"wp-image-2277\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1.png 1600w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1024x384.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-768x288.png 768w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1536x576.png 1536w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1568x588.png 1568w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/figure>\n\n\n\n<p>Google reCAPTCHA helps protect websites from spam, bot attacks, and other automated form submissions.<\/p>\n\n\n\n<p>Adding reCAPTCHA to your Magento 2 custom forms adds an essential layer of security, ensuring only genuine user interactions are processed by your site.<\/p>\n\n\n\n<p>Magento 2 already includes a built-in implementation of Google reCAPTCHA for standard frontend forms, such as:<\/p>\n\n\n\n<ol><li>Customer Login<\/li><li>Forgot Password<\/li><li>Create New Customer Account<\/li><li>Edit Customer Account<\/li><li>Contact Us<\/li><li>Product Review<\/li><li>Newsletter Subscription<\/li><li>Send to Friend<\/li><li>Checkout\/Placing Order<\/li><li>Coupon Codes, and&nbsp;<\/li><li>PayPal PayflowPro payment.<\/li><\/ol>\n\n\n\n<p>Additionally, Magento\u2019s flexibility allows us to add Google reCAPTCHA to custom forms as needed. This guide will walk you through the process of configuring Google reCAPTCHA in Magento 2, creating a custom extension for your form, and integrating reCAPTCHA into the form template.<\/p>\n\n\n\n<p><strong>How to Add Google reCAPTCHA to Custom Form in Magento 2<\/strong><\/p>\n\n\n\n<ol><li><a href=\"#Step 1\">Configure Google reCAPTCHA in Magento 2<\/a><\/li><li><a href=\"#Step 2\">Create a Custom Extension to Display Your Custom Form<\/a><\/li><li><a href=\"#Step 3\">Add the Magento 2 Custom Form to the List of Frontend reCAPTCHA<\/a><\/li><li><a href=\"#Step 4\">Integrate Google reCAPTCHA into the Custom Form Template<\/a><\/li><li><a href=\"#Step 5\">Build a Controller to Handle Custom Form Submissions<\/a><\/li><li><a href=\"#Step 6\">Validate Google reCAPTCHA for the Custom Form in Magento 2<\/a><\/li><\/ol>\n\n\n\n<div class=\"wp-block-cover\" style=\"min-height:200px\"><img loading=\"lazy\" width=\"800\" height=\"234\" class=\"wp-block-cover__image-background wp-image-2296\" alt=\"\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-2.png\" style=\"object-position:92% 39%\" data-object-fit=\"cover\" data-object-position=\"92% 39%\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-2.png 800w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-2-300x88.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-2-768x225.png 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><div class=\"wp-block-cover__inner-container\">\n<p class=\"has-text-align-center has-dark-gray-color has-text-color\" style=\"font-size:24px\"><span class=\"has-inline-color has-dark-gray-color\"><strong>Struggling<\/strong><\/span> <span class=\"has-inline-color has-dark-gray-color\"><strong>to Add Google reCAPTCHA to Custom Forms?<\/strong><\/span><\/p>\n\n\n\n<p class=\"has-text-align-center has-dark-gray-color has-text-color\">Our expert developers can help you seamlessly integrate reCAPTCHA and enhance your form security while minimizing user friction.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link has-white-color has-text-color has-background\" href=\"\/magento-development\" style=\"background-color:#e6621b\" target=\"_blank\" rel=\"magento-development noopener\">Get Help Now<\/a><\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<h2 id=\"Step 1\">Step 1. \u0421onfigure Google reCAPTCHA on Magento 2<\/h2>\n\n\n\n<p>First, register your site at <a href=\"https:\/\/www.google.com\/recaptcha\/admin\/create\" target=\"_blank\" rel=\"noreferrer noopener\">Google reCAPTCHA<\/a>, and select the type of reCAPTCHA you want to use.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"545\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1.png\" alt=\"Step 1: register your website at Google reCAPTCHA\" class=\"wp-image-2280\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1-300x136.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1-1024x463.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1-1-768x347.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<p>Then, go to<strong> Stores &gt; Configuration &gt; Security &gt; Google reCAPTCHA Storefront &gt; reCAPTCHA<\/strong> <strong>v2 (&#8220;I am not a robot&#8221;) \/ reCAPTCHA v2 Invisible \/ reCAPTCHA v3 Invisible<\/strong> and enter the site key and secret key you created on Google reCAPTCHA after registering. Make sure that the setting name in Magento matches the type of Google reCAPTCHA you created (e.g., reCAPTCHA v2 or v3).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"1190\" height=\"567\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-2.png\" alt=\"Step 1: enter the site key and secret key\" class=\"wp-image-2281\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-2.png 1190w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-2-300x143.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-2-1024x488.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-2-768x366.png 768w\" sizes=\"(max-width: 1190px) 100vw, 1190px\" \/><\/figure><\/div>\n\n\n\n<h2 id=\"Step 2\">Step 2. Create a Custom Extension to Display Your Custom Form<\/h2>\n\n\n\n<p>We need to create the following files.<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/registration.php<\/div><pre><code class=\"language-php\">&lt;?php\n\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\n\nComponentRegistrar::register(\n   ComponentRegistrar::MODULE, 'Vendor_CustomFormRecaptcha',\n   __DIR__\n);\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/etc\/module.xml<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n       xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n   &lt;module name=\"Vendor_CustomFormRecaptcha\"\/>\n&lt;\/config>\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/etc\/frontend\/routes.xml<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"standard\">\n        &lt;route id=\"customformrecaptcha\" frontName=\"customformrecaptcha\">\n            &lt;module name=\"Vendor_CustomFormRecaptcha\" \/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/Controller\/Index\/Index.php<\/div><pre><code class=\"language-php\">&lt;?php\n\n\nnamespace Vendor\\CustomFormRecaptcha\\Controller\\Index;\n\n\nuse Magento\\Framework\\App\\ActionInterface;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\n\n\nclass Index implements ActionInterface\n{\n   \/**\n    * @var PageFactory\n    *\/\n   private $resultPageFactory;\n\n\n   \/**\n    * @var Context\n    *\/\n   private $context;\n\n\n   public function __construct(\n       Context $context,\n       PageFactory $resultPageFactory\n   ) {\n       $this->context = $context;\n       $this->resultPageFactory = $resultPageFactory;\n   }\n\n\n   public function execute()\n   {\n       return $this->resultPageFactory->create();\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/view\/frontend\/layout\/customformrecaptcha_index_index.xml<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n   &lt;body>\n       &lt;referenceContainer name=\"content\">\n           &lt;block class=\"Magento\\Framework\\View\\Element\\Template\"\n                  name=\"customformrecaptcha\"\n                  template=\"Vendor_CustomFormRecaptcha::form.phtml\"\n           \/>\n       &lt;\/referenceContainer>\n   &lt;\/body>\n&lt;\/page><\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">app\/code\/Vendor\/CustomFormRecaptcha\/view\/frontend\/templates\/form.phtml<\/div><pre><code class=\"language-php\">&lt;?php \/** @var \\Magento\\Framework\\View\\Element\\Template $block *\/ ?>\n&lt;form action=\"\" method=\"post\">\n   &lt;div>\n       &lt;label for=\"name\">Name:&lt;\/label>\n       &lt;input type=\"text\" name=\"name\" id=\"name\" required\/>\n   &lt;\/div>\n   &lt;div>\n       &lt;label for=\"email\">Email:&lt;\/label>\n       &lt;input type=\"email\" name=\"email\" id=\"email\" required\/>\n   &lt;\/div>\n   &lt;div>\n       &lt;button type=\"submit\">Submit&lt;\/button>\n   &lt;\/div>\n   &lt;input name=\"form_key\" type=\"hidden\" value=\"&lt;?php echo $block->getFormKey(); ?>\" \/>\n&lt;\/form>\n<\/code><\/pre><\/div>\n\n\n\n<p>After creating the files, run the following commands:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">php bin\/magento setup:upgrade\nphp bin\/magento setup:di:compile\nphp bin\/magento setup:static-content:deploy\nphp bin\/magento cache:flush\n<\/code><\/pre><\/div>\n\n\n\n<p>Now we can see our form following the link: https:\/\/your-website-url\/customformrecaptcha\/index\/index  <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"1206\" height=\"456\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-3.png\" alt=\"Step 2. Create a Custom Extension to Display Your Custom Form\" class=\"wp-image-2282\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-3.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-3-300x113.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-3-1024x387.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-3-768x290.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure><\/div>\n\n\n\n<h2 id=\"Step 3\">Step 3. Add the Magento 2 Custom Form to the List of Frontend reCAPTCHA<\/h2>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create a file app\/code\/Vendor\/CustomFormRecaptcha\/etc\/adminhtml\/system.xml<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n   &lt;system>\n       &lt;section id=\"recaptcha_frontend\">\n           &lt;group id=\"type_for\">\n               &lt;field id=\"customformrecaptcha\" translate=\"label\" type=\"select\" sortOrder=\"600\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"0\" canRestore=\"1\">\n                   &lt;label>Enable for Custom Form Recaptcha&lt;\/label>\n                   &lt;source_model>Magento\\ReCaptchaAdminUi\\Model\\OptionSource\\Type&lt;\/source_model>\n               &lt;\/field>\n           &lt;\/group>\n       &lt;\/section>\n   &lt;\/system>\n&lt;\/config>\n<\/code><\/pre><\/div>\n\n\n\n<p>Clean magento cache.<br><code>php bin\/magento cache:flush<\/code><\/p>\n\n\n\n<p>Go to the Magento Admin panel, navigate to <strong>Stores &gt; Configuration &gt; Security &gt; Google reCAPTCHA &gt; Storefront<\/strong>, and you will see an option to enable Google reCAPTCHA for our custom form.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1199\" height=\"491\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-4-1-1.png\" alt=\"Step 3. Add the Magento 2 Custom Form to the List of Frontend reCAPTCHA\" class=\"wp-image-2292\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-4-1-1.png 1199w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-4-1-1-300x123.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-4-1-1-1024x419.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-4-1-1-768x315.png 768w\" sizes=\"(max-width: 1199px) 100vw, 1199px\" \/><\/figure>\n\n\n\n<h2 id=\"Step 4\">Step 4. Display Google reCAPTCHA in the Magento 2 Custom Form<\/h2>\n\n\n\n<p>Create a reCAPTCHA block in the layout file <code>app\/code\/Vendor\/CustomFormRecaptcha\/view\/frontend\/layout\/customformrecaptcha_index_index.xml<\/code><br>After adding the block, the content of file will be the following:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n   &lt;body>\n       &lt;referenceContainer name=\"content\">\n           &lt;block class=\"Magento\\Framework\\View\\Element\\Template\"\n                  name=\"customformrecaptcha\"\n                  template=\"Vendor_CustomFormRecaptcha::form.phtml\">\n               &lt;block class=\"Magento\\ReCaptchaUi\\Block\\ReCaptcha\"\n                      name=\"recaptcha\"\n                      after=\"-\"\n                      template=\"Magento_ReCaptchaFrontendUi::recaptcha.phtml\"\n                      ifconfig=\"recaptcha_frontend\/type_for\/customformrecaptcha\">\n                   &lt;arguments>\n                       &lt;argument name=\"recaptcha_for\" xsi:type=\"string\">customformrecaptcha&lt;\/argument>\n                       &lt;argument name=\"jsLayout\" xsi:type=\"array\">\n                           &lt;item name=\"components\" xsi:type=\"array\">\n                               &lt;item name=\"recaptcha\" xsi:type=\"array\">\n                                   &lt;item name=\"component\" xsi:type=\"string\">Magento_ReCaptchaFrontendUi\/js\/reCaptcha&lt;\/item>\n                               &lt;\/item>\n                           &lt;\/item>\n                       &lt;\/argument>\n                   &lt;\/arguments>\n               &lt;\/block>\n           &lt;\/block>\n       &lt;\/referenceContainer>\n   &lt;\/body>\n&lt;\/page>\n<\/code><\/pre><\/div>\n\n\n\n<p>Now let&#8217;s output the reCAPTCHA block in the form template.<\/p>\n\n\n\n<p>The content of the file <code>app\/code\/Vendor\/CustomFormRecaptcha\/view\/frontend\/templates\/form.phtml<\/code> will change to the following:<\/p>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div><\/div><pre><code class=\"language-php\">&lt;?php \/** @var \\Magento\\Framework\\View\\Element\\Template $block *\/ ?>\n&lt;form action=\"&lt;?= $block->getUrl('customformrecaptcha\/index\/post') ?>\" method=\"post\">\n   &lt;div>\n       &lt;label for=\"name\">Name:&lt;\/label>\n       &lt;input type=\"text\" name=\"name\" id=\"name\" required\/>\n   &lt;\/div>\n   &lt;div>\n       &lt;label for=\"email\">Email:&lt;\/label>\n       &lt;input type=\"email\" name=\"email\" id=\"email\" required\/>\n   &lt;\/div>\n   &lt;div>\n       &lt;?php echo $block->getChildHtml('recaptcha'); ?>\n   &lt;\/div>\n   &lt;div>\n       &lt;button type=\"submit\">Submit&lt;\/button>\n   &lt;\/div>\n   &lt;input name=\"form_key\" type=\"hidden\" value=\"&lt;?php echo $block->getFormKey(); ?>\" \/>\n&lt;\/form>\n<\/code><\/pre><\/div>\n\n\n\n<p>Now we can see Google reCAPTCHA on our custom form.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1206\" height=\"466\" src=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-5.png\" alt=\"Step 4. Display Google reCAPTCHA in the Magento 2 Custom Form\" class=\"wp-image-2284\" srcset=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-5.png 1206w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-5-300x116.png 300w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-5-1024x396.png 1024w, https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-5-768x297.png 768w\" sizes=\"(max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n\n\n\n<h2 id=\"Step 5\">Step 5. Build a Controller to Handle Custom Form Submissions<\/h2>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create file app\/code\/Vendor\/CustomFormRecaptcha\/Controller\/Index\/Post.php<\/div><pre><code class=\"language-php\">&lt;?php\n\n\nnamespace Vendor\\CustomFormRecaptcha\\Controller\\Index;\n\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\HttpPostActionInterface;\nuse Magento\\Framework\\Controller\\Result\\RedirectFactory;\nuse Magento\\Framework\\Message\\ManagerInterface;\nuse Magento\\Framework\\Exception\\LocalizedException;\n\n\nclass Post extends Action implements HttpPostActionInterface\n{\n   protected $resultRedirectFactory;\n   protected $messageManager;\n\n\n   public function __construct(\n       \\Magento\\Framework\\App\\Action\\Context $context,\n       RedirectFactory $resultRedirectFactory,\n       ManagerInterface $messageManager\n   ) {\n       parent::__construct($context);\n       $this->resultRedirectFactory = $resultRedirectFactory;\n       $this->messageManager = $messageManager;\n   }\n\n\n   public function execute()\n   {\n       $postData = $this->getRequest()->getPostValue();\n\n\n       if ($postData) {\n           try {\n               $this->_eventManager->dispatch('customformrecaptcha_validate_recaptcha', ['request' => $this->getRequest()]);\n               \/\/ Save data\n               $this->messageManager->addSuccessMessage(__('Form submitted successfully.'));\n           } catch (LocalizedException $e) {\n               $this->messageManager->addErrorMessage($e->getMessage());\n           } catch (\\Exception) {\n               $this->messageManager->addErrorMessage(__('An error occurred: %1', $e->getMessage()));\n           }\n       } else {\n           $this->messageManager->addErrorMessage(__('No data received.'));\n       }\n\n\n       return $this->resultRedirectFactory->create()->setPath('*\/*\/');\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<h2 id=\"Step 6\">Step 6. Create an Observer to Validate Google reCAPTCHA for the Custom Form in Magento 2<\/h2>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create file: app\/code\/CustomFormRecaptcha\/Module\/etc\/frontend\/events.xml<\/div><pre><code class=\"language-php\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework\/Event\/etc\/events.xsd\">\n   &lt;event name=\"customformrecaptcha_validate_recaptcha\">\n       &lt;observer name=\"validate_recaptcha\" instance=\"Vendor\\CustomFormRecaptcha\\Observer\\ValidateRecaptcha\" \/>\n   &lt;\/event>\n&lt;\/config>\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"wp-block-prismatic-blocks\"><div class=\"prism-title\">Create file: app\/code\/Vendor\/CustomFormRecaptcha\/Observer\/ValidateRecaptcha.php<\/div><pre><code class=\"language-php\">\n&lt;?php\n\n\nnamespace Vendor\\CustomFormRecaptcha\\Observer;\n\n\nuse Magento\\Framework\\Event\\Observer;\nuse Magento\\Framework\\Event\\ObserverInterface;\nuse Magento\\Framework\\Exception\\InputException;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Framework\\ObjectManagerInterface;\nuse Magento\\ReCaptchaUi\\Model\\IsCaptchaEnabledInterface;\nclass ValidateRecaptcha implements ObserverInterface\n{\n   \/**\n    * @var IsCaptchaEnabledInterface\n    *\/\n   private $isCaptchaEnabled;\n\n\n   \/**\n    * @var ObjectManagerInterface\n    *\/\n   private $objectManager;\n\n\n   public function __construct(\n       IsCaptchaEnabledInterface $isCaptchaEnabled,\n       ObjectManagerInterface $objectManager,\n   ) {\n       $this->isCaptchaEnabled = $isCaptchaEnabled;\n       $this->objectManager = $objectManager;\n   }\n\n\n   \/**\n    * @throws InputException\n    *\/\n   public function execute(Observer $observer)\n   {\n       $key = 'customformrecaptcha';\n       $request = $observer->getRequest();\n       if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) {\n\n\n           $validationConfigResolver = $this->objectManager->get(\n               \\Magento\\ReCaptchaUi\\Model\\ValidationConfigResolverInterface::class\n           );\n\n\n           $captchaResponseResolver = $this->objectManager->get(\n               \\Magento\\ReCaptchaUi\\Model\\CaptchaResponseResolverInterface::class\n           );\n\n\n           $captchaValidator = $this->objectManager->get(\n               \\Magento\\ReCaptchaValidationApi\\Api\\ValidatorInterface::class\n           );\n\n\n           $validationConfig = $validationConfigResolver->get($key);\n\n\n           try {\n               $reCaptchaResponse = $captchaResponseResolver->resolve($request);\n           } catch (InputException $e) {\n               throw new LocalizedException(__('Please complete the reCAPTCHA validation.'));\n           }\n\n\n           $validationResult = $captchaValidator->isValid($reCaptchaResponse, $validationConfig);\n           if (false === $validationResult->isValid()) {\n               throw new LocalizedException(__('reCAPTCHA validation failed. Please try again.'));\n           }\n       }\n   }\n}\n<\/code><\/pre><\/div>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>By taking these simple steps, you improve not just the functionality of your website but also the confidence your customers have in your brand. Keep optimizing and securing your store to provide the best possible experience for your users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google reCAPTCHA helps protect websites from spam, bot attacks, and other automated form submissions. <\/p>\n<p>Adding reCAPTCHA to your Magento 2 custom forms adds an essential layer of security, ensuring only genuine user interactions are processed by your site.<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[144],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Add Google reCAPTCHA to Custom Form\u200b in Magento 2<\/title>\n<meta name=\"description\" content=\"Learn how to add Google reCAPTCHA to custom forms in Magento 2 to protect your site from spam and bots with this step-by-step guide.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/plumrocket.com\/learn\/magento-2-add-google-recaptcha\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Add Google reCAPTCHA to Custom Form\u200b in Magento 2\" \/>\n<meta property=\"og:description\" content=\"Learn how to add Google reCAPTCHA to custom forms in Magento 2 to protect your site from spam and bots with this step-by-step guide.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/plumrocket.com\/learn\/magento-2-add-google-recaptcha\" \/>\n<meta property=\"og:site_name\" content=\"Magento Tutorials for Beginners &amp; Professionals\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-10T11:47:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-18T08:08:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/plumrocket.com\/learn\/wp-content\/uploads\/2025\/01\/add-google-recaptcha-to-custom-form-1.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/2264"}],"collection":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/comments?post=2264"}],"version-history":[{"count":14,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/2264\/revisions"}],"predecessor-version":[{"id":2566,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/posts\/2264\/revisions\/2566"}],"wp:attachment":[{"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/media?parent=2264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/categories?post=2264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/plumrocket.com\/learn\/wp-json\/wp\/v2\/tags?post=2264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}