I don't really know PHP, but I need to add a Google Recaptcha to a form and I'm not being able to do it.
I have a form being add as a shortcode on a Wordpress that send the information to my CRM as an action link.
I've tried following this solution new google recaptcha with checkbox server side php but with my limited knowledge, it still doesn't work.
I've already added the script in the head of the website as asked in here: http://codeforgeek.com/2014/12/google-recaptcha-tutorial/
<?php
function demo_ax_manufacturing_shortcode($atts, $content = null) {
global $post;
$atts = shortcode_atts(array("action" => 'https://analytics.clickdimensions.com/forms/h/XXXXXXXXX'), $atts);
$home = get_page_by_title("Home");
if (isset($_POST['g-recaptcha-response'])) {
$captcha=$_POST['g-recaptcha-response'];
}
if (!$captcha) {
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "I'M ADDING MY SECRET KEY IN HERE";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if ($responseKeys["success"]) {
return '<form class="wpcf7-form demo-ax-manufacturing custom-form" action="'.$atts['action'].'" method="post">';
} else {
echo '<h2>You are spammer!</h2>';
}
<p class="form-text"><span class="wpcf7-form-control-wrap first-name"><input id="txtfirstname" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required first-name required" name="txtfirstname" size="40" type="text" value="" placeholder="'.get_field("first_name", $home->ID).'*" /></span></p>
<p class="form-text"><span class="wpcf7-form-control-wrap last-name"><input id="txtlastname" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required last-name required" name="txtlastname" size="40" type="text" value="" placeholder="'.get_field("last_name", $home->ID).'*" /></span></p>
<p class="form-text"><span class="wpcf7-form-control-wrap email"><input id="Email" class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-required wpcf7-validates-as-email email required" name="Email" size="40" type="email" value="" placeholder="'.get_field("email", $home->ID).'*" /></span></p>
<p class="form-text"><span class="wpcf7-form-control-wrap company"><input id="txtcompanyname" class="wpcf7-form-control wpcf7-text company required" name="txtcompanyname" size="40" type="text" value="" placeholder="'.get_field("company", $home->ID).'*" /></span></p>
<p class="text-center"><span class="wpcf7-form-control wpcf7-checkbox checkbox-874"><input id="subnewsletter" name="subnewsletter" type="checkbox" value="Yes" /> <span class="wpcf7-list-item-label">'.get_field("subnewsletter", $home->ID).'</span></span></span></p>
<p class="text-center"><span class="wpcf7-form-control-wrap acceptance-575"><span class="wpcf7-form-control wpcf7-acceptance"><span class="wpcf7-list-item"><input id="termsandconditions" name="termsandconditions" type="checkbox" value="Yes" aria-required="true" aria-invalid="false" wpcf7-validates-as-required/> <span class="wpcf7-list-item-label">'.get_field("termsandconditions", $home->ID).'</span></span></span></p>
<div class="g-recaptcha" data-sitekey="I AM ADDING MY SITE KEY IN HERE"></div>
<input type="hidden" name="pageurl" id="pageurl" value="'.get_permalink($post->ID).'" />
<input type="hidden" name="country" id="country" value="'.get_country_code().'" />
<p class="text-center"><input class="wpcf7-form-control wpcf7-submit" type="submit" value="'.get_field("submit", $home->ID).'" /></p>
</form>';
}
add_shortcode('demo-ax-manufacturing-form', 'demo_ax_manufacturing_shortcode');
Any ideas on how to solve this?