{"id":12402,"date":"2025-01-06T09:00:00","date_gmt":"2025-01-06T09:00:00","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=12402"},"modified":"2025-01-07T15:29:33","modified_gmt":"2025-01-07T15:29:33","slug":"how-to-add-google-recaptcha-v3-to-a-form-php-html","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/how-to-add-google-recaptcha-v3-to-a-form-php-html\/","title":{"rendered":"How to add Google reCAPTCHA v3 to a Form (PHP\/HTML)"},"content":{"rendered":"\n<p>Google&#8217;s reCAPTCHA v2 made the user interact with a box (<a href=\"https:\/\/www.bronco.co.uk\/our-ideas\/how-to-add-google-recaptcha-to-a-form-phphtml\/\">see our integration guide<\/a>); having to click &#8220;I&#8217;m not a robot&#8221; was annoying and hindered conversion rates. v3 removes that, working behind the scenes to calculate whether a bot or human is completing the form. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1<\/h3>\n\n\n\n<p>Sign up and get your keys here: <a href=\"https:\/\/www.google.com\/recaptcha\/admin\">https:\/\/www.google.com\/recaptcha\/admin<\/a>; make sure to select &#8220;v3&#8221;. You will get a SITE key and a SECRET key. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2<\/h3>\n\n\n\n<p>Include this on your page:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;script src=&quot;https:\/\/www.google.com\/recaptcha\/api.js?render=SITE_KEY&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    grecaptcha.ready(function() {\n        grecaptcha.execute(&#39;SITE_KEY&#39;, {action: &#39;contact_us&#39;}).then(function(token) {\n            \/\/ Send the token to your server\n            document.getElementById(&#39;recaptcha-token&#39;).value = token;\n        });\n    });\n&lt;\/script&gt;<\/code><\/pre><\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<p>Where &#8220;action&#8221; is whatever descriptive term you want to give the page\/type of form you&#8217;re working with and SITE_KEY is the site key that was generated in Step 1. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3<\/h3>\n\n\n\n<p>Add the following into your form:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;input type=&quot;hidden&quot; name=&quot;recaptcha_token&quot; id=&quot;recaptcha-token&quot;\/&gt;<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4<\/h3>\n\n\n\n<p>On form submission do this:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>$response = file_get_contents(&quot;https:\/\/www.google.com\/recaptcha\/api\/siteverify?secret=SECRET_KEY&response=&quot;.$_POST[&#39;recaptcha_token&#39;]);\n$responseData = json_decode($response);\n\nif($responseData-&gt;success && $responseData-&gt;score &gt;= 0.5){\n    \/\/ User is likely human; process form submission\n}else{\n    \/\/ Possible bot or suspicious activity; abort form submission\n}<\/code><\/pre><\/div>\n\n\n\n<p>Where SECRET_KEY is the secret key generated in Step 1. You can tweak the score to suit; 0 means it&#8217;s definitely a bot and 1 means it&#8217;s definitely a human. <\/p>\n\n\n\n<p>Enjoy!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google&#8217;s reCAPTCHA v2 made the user interact with a box (see our integration guide); having to click &#8220;I&#8217;m not a robot&#8221; was annoying and hindered conversion rates. v3 removes that, working behind the scenes to calculate whether a bot or human is completing the form. Step 1 Sign up and get your keys here: https:\/\/www.google.com\/recaptcha\/admin; [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":12410,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-12402","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=12402"}],"version-history":[{"count":7,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12402\/revisions"}],"predecessor-version":[{"id":12418,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/12402\/revisions\/12418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media\/12410"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=12402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=12402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}