0

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?

Pratik Gadoya
  • 1,420
  • 1
  • 16
  • 27
lela_rib
  • 147
  • 2
  • 10
  • Such a CAPTCHA doing its job involves two steps; _showing_ the CAPTCHA to the user is only one half of it - the other half is verifying that the user’s response was actually correct, when you _process_ the submitted form data. But that won’t work here in a scenario like this, where you submit the form to an external, 3rd-party(?) URL - _they_ would have to perform that part of verifying the response then, but apart from that this is probably not implemented on their end, the CAPTCHA script also uses the session to store data, and they don’t have access to your site’s session either. […] – 04FS Oct 23 '19 at 12:10
  • […] What you would have to do here, is submit the form to your own system first, validate the CAPTCHA - and then have your system make a POST request on the server side, sending the rest of the data to the external URL. But that then still leaves you with having to deal with that site’s _response_ to the form submission, and pass that back to your user somehow. (And if that site required further user interaction after the intial data was send, then things would get really complex ...) – 04FS Oct 23 '19 at 12:10
  • I think you'd better just to use the reCaptcha plugin for wordpress https://contactform7.com/recaptcha/ – Keyboard ninja Oct 23 '19 at 12:42

0 Answers0