Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample WordPress page for validating a Securimage-WP CAPTCHA from shortcode
<h3>Securimage-WP Example Form</h3>
I am a WordPress page with a form containing a few simple fields including a CAPTCHA using the Securimage-WP WordPress plugin.
I utilize the shortcode <code>&#x5B;siwp_show_captcha&#x5D;</code> to display a CAPTCHA, and then validate it by calling <code>siwp_check_captcha()</code>.
For this to work as a WordPress page, a plugin for executing PHP code within WordPress pages (i.e. Exec-PHP) will need to be installed so PHP code can be run from a page. You can also post the form to a standalone PHP script for validation as long as it has access to the WordPress code (includes wp-load.php somewhere in the script).
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$values = array();
$errors = array();
$values['name'] = @trim(stripslashes($_POST['contact_name']));
$values['email'] = @trim(stripslashes($_POST['email']));
$values['message'] = @trim(stripslashes(strip_tags($_POST['message'])));
if (empty($values['name'])) $errors['contact_name'] = 'Please enter your name';
if (!preg_match('/^(?:[\w\d-]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $values['email'])) $errors['email'] = 'The email address supplied is invalid';
if (strlen($values['message']) < 20) $errors['message'] = 'Please enter a message longer than 20 characters';
if (sizeof($errors) == 0) {
if (function_exists('siwp_check_captcha')) {
// make sure plugin is enabled before calling function
if (false == siwp_check_captcha($err)) {
$errors['captcha'] = $err;
}
}
}
if (sizeof($errors) > 0) {
show_form($errors, $values);
} else {
// form code goes here, no errors & captcha was correct
echo "<span style='font-size: 1.2em'><strong><em>Congrats, you win the captcha solving challenge!</em></strong>";
}
} else {
show_form();
}
?>
<?php function show_form($errors = array(), $values = array()) { ?>
<?php if (sizeof($errors) > 0): ?>
<p>There was a problem with your submission. Please correct the following errors:</p>
<ul>
<?php foreach($errors as $error): ?>
<li><?php echo $error ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div>
<label for="contact_name">Your Name:
<input type="text" name="contact_name" id="contact_name" class="input" value="<?php echo htmlspecialchars(stripslashes(@$values['name'])) ?>" size="20" /></label>
</div>
<br />
<div>
<label for="email">E-mail:
<input type="email" name="email" id="email" class="input" value="<?php echo htmlspecialchars(stripslashes(@$values['email'])) ?>" size="25" /></label>
</div>
<br />
<div>
<label for="message">Message:<br /><pre style="border: 0; margin: 0; padding: 0"><textarea name="message" id="message" class="input" rows="8" style="width: 100%"><?php echo htmlspecialchars(stripslashes(@$values['message'])) ?></textarea></pre></label>
</div>
<br />
[siwp_show_captcha]
<p>&nbsp;</p>
<p>
<input type="submit" value="Send Message" />
</p>
</form>
<?php } // end function show_form ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.