{"id":1789,"date":"2015-07-31T12:28:49","date_gmt":"2015-07-31T11:28:49","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=1789"},"modified":"2025-01-06T09:28:07","modified_gmt":"2025-01-06T09:28:07","slug":"how-to-add-google-recaptcha-to-a-form-phphtml","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/how-to-add-google-recaptcha-to-a-form-phphtml\/","title":{"rendered":"How to add Google reCAPTCHA to a Form (PHP\/HTML)"},"content":{"rendered":"\n<p>When you need to add a relatively simple new feature like this, if you&#8217;re anything like me, you don&#8217;t want to be reading through pages and pages of documentation; you want an example you can implement quickly then edit as you see fit. I couldn&#8217;t find any such thing when I was integrating, so hopefully these 4 simple steps will help you.<br><\/p>\n\n\n\n<p>Please note, this guide relates to <strong>reCAPTCHA v2<\/strong>. Find <a href=\"https:\/\/www.bronco.co.uk\/our-ideas\/how-to-add-google-recaptcha-v3-to-a-form-php-html\/\">a guide to reCAPTCHA v3 here<\/a>. <\/p>\n\n\n\n<p><strong>Also note you will need to understand basic PHP programming to follow this guide.<\/strong><\/p>\n\n\n\n<p><\/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> (you will get a SITE key and a SECRET key, used later)<\/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=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;script src=&quot;https:\/\/www.google.com\/recaptcha\/api.js&quot;&gt;&lt;\/script&gt;<\/code><\/pre><\/div>\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;div class=&quot;g-recaptcha brochure__form__captcha&quot; data-sitekey=&quot;YOUR SITE KEY&quot;&gt;&lt;\/div&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>$recaptcha = $_POST[&#39;g-recaptcha-response&#39;];\n$res = reCaptcha($recaptcha);\nif($res[&#39;success&#39;]){\n  \/\/ Send email\n}else{\n  \/\/ Error\n}<\/code><\/pre><\/div>\n\n\n\n<p>Using the following function:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function reCaptcha($recaptcha){\n  $secret = &quot;YOUR SECRET KEY&quot;;\n  $ip = $_SERVER[&#39;REMOTE_ADDR&#39;];\n\n  $postvars = array(&quot;secret&quot;=&gt;$secret, &quot;response&quot;=&gt;$recaptcha, &quot;remoteip&quot;=&gt;$ip);\n  $url = &quot;https:\/\/www.google.com\/recaptcha\/api\/siteverify&quot;;\n  $ch = curl_init();\n  curl_setopt($ch, CURLOPT_URL, $url);\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n  curl_setopt($ch, CURLOPT_TIMEOUT, 10);\n  curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);\n  $data = curl_exec($ch);\n  curl_close($ch);\n\n  return json_decode($data, true);\n}<\/code><\/pre><\/div>\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>When you need to add a relatively simple new feature like this, if you&#8217;re anything like me, you don&#8217;t want to be reading through pages and pages of documentation; you want an example you can implement quickly then edit as you see fit. I couldn&#8217;t find any such thing when I was integrating, so hopefully [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-1789","post","type-post","status-publish","format-standard","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/1789","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=1789"}],"version-history":[{"count":2,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/1789\/revisions"}],"predecessor-version":[{"id":12406,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/1789\/revisions\/12406"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=1789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=1789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}