Last active
May 10, 2016 12:35
-
-
Save cdillon/31b4ccc4185dbd4b778b to your computer and use it in GitHub Desktop.
example of a custom form in WordPress using math Captcha plugin by BestWebSoft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// shortcode: [demo-form] | |
function demo_form_shortcode( $atts ) { | |
$name = ''; | |
$email = ''; | |
$company = ''; | |
$errors = array(); | |
if ( isset( $_POST['demo_form_submitted'] ) | |
&& wp_verify_nonce( $_POST['demo_form_submitted'], 'demo_submission_form' ) ) { | |
// sanitize posted values | |
$name = sanitize_text_field( $_POST['demo_name'] ); | |
$email = sanitize_text_field( $_POST['demo_email'] ); | |
$company = sanitize_text_field( $_POST['demo_company'] ); | |
// using Captcha plugin | |
if ( function_exists( 'cptch_check_custom_form' ) && cptch_check_custom_form() !== true ) { | |
$errors['captcha'] = 'Please complete the CAPTCHA.'; | |
} | |
// validate fields | |
if ( empty( $name ) ) { | |
$errors['name'] = 'Please enter your name.'; | |
} | |
if ( empty( $email ) ) { | |
$errors['email'] = 'Please enter your email address.'; | |
} | |
if ( ! count( $errors ) ) { | |
// do something with submitted data | |
return '<div class="success">' . __( 'Hey, thanks ' . $name . '!', 'yourtextdomain' ) .'</div>'; | |
} | |
} | |
// The form | |
ob_start(); | |
?> | |
<div id="demo-form"> | |
<p class="required-notice"><span class="required symbol"></span><?php _e( 'Required Field', 'yourtextdomain' ); ?></p> | |
<form id="demo-submission-form" method="post" action=""> | |
<?php echo wp_nonce_field( 'demo_submission_form', 'demo_form_submitted' ); ?> | |
<p class="form-field"> | |
<label for="demo_name"><?php _e( 'Full Name', 'yourtextdomain' ); ?></label><span class="required symbol"></span> | |
<input id="demo_name" class="text" type="text" name="demo_name" value="<?php echo $name; ?>" minlength="2" required> | |
<?php if ( isset( $errors['name'] ) ) : ?> | |
<span class="error"><label class="error"><?php echo $errors['name']; ?></label></span> | |
<?php endif; ?> | |
</p> | |
<p class="form-field"> | |
<label for="demo_email"><?php _e( 'Email', 'yourtextdomain' ); ?></label> | |
<input id="demo_email" class="text email" type="email" name="demo_email" value="<?php echo $email; ?>"> | |
<?php if ( isset( $errors['email'] ) ) : ?> | |
<span class="error"><label class="error"><?php echo $errors['email']; ?></label></span> | |
<?php endif; ?> | |
</p> | |
<p class="form-field"> | |
<label for="demo_company"><?php _e( 'Company', 'yourtextdomain' ); ?></label> | |
<input id="demo_company" class="text" type="text" name="demo_company" value="<?php echo $company; ?>"> | |
</p> | |
<div class="demo-captcha"> | |
<label for="demo_captcha"><?php _e( 'Captcha', 'yourtextdomain' ); ?></label><span class="required symbol"></span> | |
<div class="wrap"> | |
<?php | |
// using Captcha plugin | |
if ( function_exists( 'cptch_display_captcha_custom' ) ) { | |
?><input type="hidden" name="cntctfrm_contact_action" value="true"><?php | |
echo cptch_display_captcha_custom(); | |
} | |
?> | |
<?php if ( isset( $errors['captcha'] ) ) : ?> | |
<p><label class="error"><?php echo $errors['captcha']; ?></label></p> | |
<?php endif; ?> | |
</div> | |
</div> | |
<p class="form-field"> | |
<input type="submit" id="demo_submit" name="demo_submit" value="<?php _e( 'submit', 'yourtextdomain' ); ?>" class="button" validate="required:true" /> | |
</p> | |
</form> | |
</div><!-- demo-form --> | |
<?php | |
$html = ob_get_contents(); | |
ob_end_clean(); | |
return $html; | |
} | |
add_shortcode( 'demo-form', 'demo_form_shortcode' ); |
This plugin just generates a combination of text and numbers. It's less secure than using an image generator tool. That's my understanding. Are there any alternatives that rely on image-based math captcha?
wp_nonce_field doesn't need to be echoed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for the late response. I did not receive any notification.
Jetpack should be running it through Akismet. Are you getting a lot of spam anyway?