Support » Fixing WordPress » Can't get reCaptcha to work in page template.

  • I have a form that works beautifully. I’ve started getting loads of spam so decided to add reCaptcha to fix this. The client actually asked for reCaptcha specifically so it’s got to be reCaptcha. The forms are hardcoded into my theme’s template files (not added with a plugin). The problem is I can’t get reCaptcha to work in the page template files. I’ve followed the tutorials available however wherever I enter the ‘require_once’ piece of code (see snippet) within my template is where the code stops being output.

    <script type="text/javascript">
     var RecaptchaOptions = {
        theme : 'blackglass'
    <form class="enquiry" action="/private/contact-response.php" method="post" onsubmit="return formValidator()">
    		<ul class="plain-list">
    			<label for="name">Name</label>
    			<input id="name" name="name" class="text" type="text" />
    			<label for="email">Email address</label>
    			<input id="email" name="email" class="text" type="text" />
    			<label for="phone">Telephone number</label>
    			<input id="phone" name="phone" class="text" type="text" />
    			<select name="typeofenquiry" id="typeofenquiry">
    			<option value=" Type of booking enquiry " class="head-option">Type of booking enquiry</option>
    			<option value=" Surfing Lessons ">Surfing Lessons</option>
       		 	<option value=" Quest Coasteering ">Quest Coasteering</option>
       		 	<option value=" Equipment Hire ">Equipment Hire</option>
    			<option value=" Roxy Girl Days ">Roxy Girl Days</option>
    			<option value=" Shopping Enquiry ">Shopping Enquiry</option>
    			<option value=" Stag & Hen Activities ">Stag & Hen Activities</option>
    			<option value=" General Enquiry ">General Enquiry</option>
    			<textarea id="message" name="message" class="text" onclick="if(this.value == 'Additional Info') this.value='';" onblur="if(this.value.length == 0) this.value='Additional Info';" rows="" cols="" >Additional Info</textarea>
    $publickey = "xxxxxxxxxxxxxxxxxxx"; // you got this from the signup page
    echo recaptcha_get_html($publickey);
    			<li style="clear:both;">
    			<input class="send" type="submit" value="Submit Enquiry" />

    Then there’s the code in contact-response.php …

     $privatekey = "xxxxxxxxxxxxxxxx";
     $resp = recaptcha_check_answer ($privatekey,
     if (!$resp->is_valid) {
       // What happens when the CAPTCHA was entered incorrectly
       die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
            "(reCAPTCHA said: " . $resp->error . ")");
     } else {
       // Your code here to handle a successful verification
    //start building the mail string
    $msg = "Name:    $_POST[name]\n";
    $msg .= "Email Address: $_POST[email]\n";
    $msg .= "Telephone number: $_POST[phone]\n";
    $msg .= "Type of Enquiry: $_POST[typeofenquiry]\n";
    $msg .= "Message:    $_POST[message]\n";
    $thankyoupage = "/contact-thankyou.php";
    //set up the mail
    $recipient = "";  //the inbox where the sent mail goes.
    $subject = "Website enquiry";
    $mailheaders = "From:  <> \n";
    $mailheaders .= "Reply-To: $_POST[email]";
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    header("Location: $thankyoupage");

    Both contact-response.php and recaptchalib.php in are located in ‘private’ folder at root. The page template is obviously in the theme folder. The form and other validation works fine without the reCaptcha code. I look forward to some assistance on this – I’m stuck!

  • The topic ‘Can't get reCaptcha to work in page template.’ is closed to new replies.