Using WP-reCAPTCHA plugin in your own wordpress plugin

Here are details on the existing plugin: http://wordpress.org/extend/plugins/wp-recaptcha/ 

When enabled, this ads a recaptcha box on all comment fields. If you have a plugin that requires anti spam checking, why not use this existing plugin.

So here is a quick howto on checking if the wp-recaptcha plugin is installed, and if it is, using it in your own custom wordpress plugin.

 

Generally the plugin will be submitting data in a form, so we have to add the re-captcha code within the plugin form like so:

<form action="" method="post">
.... existing form data here ....  
<?php
// display the re-captcha box if re-captcha is installed:
if(function_exists('display_recaptcha')){
display_recaptcha();
}
?>
</form> 

now in the code that handles this form submit, we have to check if the re-captcha result was correct, something like this:

// if recaptcha exists and there are no current errors: 
if(function_exists('check_recaptcha_new') && !$errors->get_error_codes()){
$errors = check_recaptcha_new($errors);
}
if($errors->get_error_codes()){
// an error occurred with the re-captcha or somewhere else in the plugin
echo implode($errors->get_error_messages(),'<br/>');  
}else{
// re-captcha worked and there were no other errors.
// do stuff...

Leave a Reply

Your email address will not be published. Required fields are marked *