WordPress: Using Really Simple Captcha

Tue, Jun 1, 2010

Wordpress Plugins

All WordPress plugin developers, you can integrate a security captcha image into your own WordPress plugin using the Really Simple Captcha plugin. It is a plugin in itself but it serves as an API and doesn’t have any purpose on its own without other plugins using it.

This is an example of how to use the Really Simple Captcha plugin API with your own WordPress plugin.

<?php

$captcha_correct = false; //set it to false until proven true
require_once(ABSPATH . ‘wp-admin/admin-functions.php’); //include the WordPress admin functions file

if (is_plugin_active(plugin_basename(‘really-simple-captcha/really-simple-captcha.php’))) { //check if the plugin is installed and active
    if (class_exists(‘ReallySimpleCaptcha’)) { //check if the Really Simple Captcha class is available
        $captcha = new ReallySimpleCaptcha();

        if (!empty($_POST)) { //data has been posted by the user, lets check whats going on
            if ($captcha -> check($_POST[‘captcha_prefix’], $_POST[‘captcha_code’])) {
                $captcha_correct = true; //the captcha has been proven as correct
            }
        } else {
            $captcha_word = $captcha -> generate_random_word(); //generate a random string with letters
            $captcha_prefix = mt_rand(); //random number
            $captcha_image = $captcha -> generate_image($captcha_prefix, $captcha_word); //generate the image file. it returns the file name
            $captcha_file = rtrim(get_bloginfo(‘wpurl’), ‘/’) . ‘/wp-content/plugins/really-simple-captcha/tmp/’ . $captcha_image; //construct the absolute URL of the captcha image
        }
    }
}

?>

<?php if ($captcha_correct == true) : ?>
    <p><?php _e(‘The captcha code you filled in is correct!’); ?>
<?php endif; ?>

<!– User Form –>
<form action="" method="post">
    <p><?php _e(‘Please fill in the code below’); ?></p>
    <input type="text" name="captcha_code" value="<?php echo esc_attr(stripslashes($_POST['captcha_code'])); ?>" />
    <input type="hidden" name="captcha_prefix" value="<?php echo $captcha_prefix; ?>" />
    <input type="submit" name="submit" value="<?php _e(‘Submit’); ?>" />
</form>

, , , , , , , , ,
lightbox-js-widejpg

This post was written by:

- who has written 71 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

7 Comments For This Post

  1. zack@brother toner cartridges Says:

    Using simply captcha designed by Takayuki Miyoshi is a great plugin.It is originally created for Contact Form 7 but you can use it with your own plugin. I believe wordpress die hard fan should try this plugin. I really like the plugin

  2. Ernstings Family Says:

    Thanks for youre posting, i have never heard of it and i will def. integrate this plugin.
    best wishes Jan

  3. Asif Iqbal Says:

    Thanks for this trick. I use it in my blog and made little customization but it is really helpful.

    Thank you very much…

  4. myturkeyphoto Says:

    Hello, do you need new module for antispam?

  5. nike Says:

    Thanks for youre posting, i have never heard of it and i will def. integrate this plugin.
    best wishes Jan

  6. Steve Marston Says:

    Even simpler PHP for wordpress for this:

    check($the_prefix, $the_captcha);
    if($correct==True)
    {
    echo “Thats right.”;
    // replace the above with whatever you want to do if they typed it right
    }
    else
    {
    echo “The Value typed for the captcha is not correct.”;
    }
    }

    $word = $captcha_instance->generate_random_word();
    $prefix = mt_rand();
    $captcha_instance->generate_image($prefix, $word);

    // note change yoururl
    $thepic = “http://yoururl/wp-content/plugins/really-simple-captcha/tmp/” . $prefix . “.png”;

    echo “”;
    echo “”;
    echo “”;
    echo “Please enter the code.”;
    echo “”;
    echo “”;
    echo “”;
    echo “”;
    echo “”;
    echo “”;

    ?>

  7. BobbyB Says:

    If you use this on a contact page and reload the page it will create *.png and *.php files on each reload.

    Anyway to delete those files so it doesn’t fill up the “tmp” folder ?

    $captcha_instance->remove($prefix) function can be used on the actual form submit only as it removes the image and code if used anywhere else in the page.

    Any ideas ?

1 Trackbacks For This Post

  1. Using Really Simple CAPTCHA Plugin for Comments » cb.blog Says:

    [...] Miyoshi created Really Simple CAPTCHA as a stand-alone plugin, complete with API, to facilitate other plugins’ incorporation of CAPTCHA functionality. I had been using the excellent Math Comment Spam Protection plugin for my [...]

Leave a Reply