Skip to content

Instantly share code, notes, and snippets.

@cdillon
Last active May 10, 2016 12:35
Show Gist options
  • Save cdillon/31b4ccc4185dbd4b778b to your computer and use it in GitHub Desktop.
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
// 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' );
@maxpen
Copy link

maxpen commented May 29, 2014

Nice gist but how would you make it work with contact form plugins like the one from Jetpack?

@cdillon
Copy link
Author

cdillon commented Jun 19, 2014

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?

@alxvallejo
Copy link

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?

@MarcGuay
Copy link

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