MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

reCAPTCHA on WordPress

with 7 comments

I put up a contact page (@Contact Me) for two reasons. Too many folks wanted to ask something and could only do so through a comment. I would review the comment, email them, et cetera. Reason two is simpler, I should have done it from the outset.

There were a few gotchas beyond installing and securing your public and private keys. I figured that it might be helpful to note them here, especially if I have to troubleshoot it later. ;-)

  1. You need create a template, like contact.php script, which should include your contact form. You’ll also embed some reCAPTCHA PHP inside this file.

Make sure that your action attribute points to the following location, which is discussed later in step 5.

<?php
<form action="/wp-content/themes/<theme name>/contact_code.php" class="contact" method="post">
?>

The reCAPTCHA PHP should be the last element in your form tag set.

<?php
  // Include the reCAPTCHA library, this assumes a relative directory.
  require_once('recaptchalib.php');
 
  // Get a key from http://recaptcha.net/api/getkey
  $publickey = "<your public key value goes here>";
 
  // The response from reCAPTCHA
  $resp = null;
  // The error code from reCAPTCHA, if any
  $error = null;
 
  echo recaptcha_get_html($publickey, $error);
?>
  1. You need to put the following WordPress template stub at the beginning of your contact.php page.
<?php
  /*
    Template Name: contact
  */
?>
  1. After you create and position the contact.php file in the /wp-content/themes/<theme_name>/ within the document root, you should create a new blog page. You don’t put any text in the page, rather you simply choose the contact.php from your list of templates. You also need a title for the page, I used @contact-me (the @ won’t be used in any reference in the URL), which is an alias to the page
  1. You need to put the balance of your PHP code in a separate file, like contact_code.php and put the URL re-write and reCAPTCHA calls here.
<?php
  // Include the library for reCAPTCHA at the theme level in the file hierarchy.
  require_once('recaptchalib.php');
 
  // Put any form PHP here.
 
  // Declare your private key.
  $privatekey = "<put your private key here>";
 
  // Check whether there a reCAPTCHA response?
  if ($_POST["recaptcha_response_field"]) {
    $resp = recaptcha_check_answer($privatekey
                                  ,$_SERVER["REMOTE_ADDR"]
                                  ,$_POST["recaptcha_challenge_field"]
                                  ,$_POST["recaptcha_response_field"]);
    // Check for valid response.
    if ($resp->is_valid) {
      if(!$errors) {
        $to = '<email here>@<domain here>';
        $subject = $subject;
        $message = "From: ".$name."\nMessage: ".$message;
        $from = 'From:'.$email;
        mail($to,$subject,$message,$from);
 
        // Set the header to the document root when successful.
        header("Location: /");
      }
    }
    else {
      // Set the header to the virtual path for the contact page.
      header("Location: /contact-me/");
      $error = $resp->error;
    }
  }
?>

That’s about it. If I forgot something, you’ll let me know.

Written by maclochlainn

July 18th, 2009 at 10:08 pm

Posted in PHP,WordPress

7 Responses to 'reCAPTCHA on WordPress'

Subscribe to comments with RSS or TrackBack to 'reCAPTCHA on WordPress'.

  1. You could do that, or since you are using WordPress you could use the contact form 7 plugin that uses captcha via the really simple CAPTCHA plugin. Easily customizable and already built leaving more time to do other things. Have a great day. ;)
    http://wordpress.org/extend/plugins/contact-form-7/
    http://wordpress.org/extend/plugins/really-simple-captcha/

    Mike Grace

    18 Jul 09 at 11:26 pm

  2. Here is the link on how to get the captcha plugin to work with the contact form. The captcha isn’t as strong but it is easy for the less ambitious.

    http://ideasilo.wordpress.com/2007/10/07/contact-form-7-15/

    Mike Grace

    18 Jul 09 at 11:36 pm

  3. I knew about the comment plug-in but didn’t know about the other. Thanks for the links. :-)

    maclochlainn

    18 Jul 09 at 11:56 pm

  4. Hi there,
    I have tried to follow your example, but for some reason I’m not able to get this to work. I don’t want to use reCAPTCHA for comment spam (I’m already using that plugin), but instead I’m trying to keep spam from a job posting entry form.

    When I try to configure the ‘require_once’ in the template page, on the output page I ether get nothing at all, or I get an error:

    Warning: require_once(/wp/wp-content/themes/wsba_theme/recaptchalib.php) [function.require-once]: failed to open stream: No such file or directory in /home/bmet_admin/bmet.org/wp/wp-content/themes/wsba_theme/jobenter.php on line 235

    Any help would be appreciated.

    Geoff

    28 Oct 09 at 1:57 am

  5. It’s not finding the library, I’d suggest you replace this:

    require_once(/wp/wp-content/themes/wsba_theme/recaptchalib.php);

    with this:

    require_once(recaptchalib.php);

    maclochlainn

    3 Nov 09 at 8:42 pm

  6. Hi, please send me recaptcha lib source =, thanks so much!

    Best regards,
    King Dang

    King Dang

    2 Aug 12 at 10:07 am

  7. I think you wanted me to post the recaptchalib.php code because the old link became invalid. It’s not my code, so all I should provide is a link. It’s also refreshed in the blog post above.

    maclochlainn

    2 Aug 12 at 10:25 am

Leave a Reply