Add A Security Question to the Register Form

Want to allow people to register on your site, but don’t want to be flooded with spam users? Try this:

resize_1316148334

add_action( 'register_form', 'add_register_field' );
function add_register_field() { ?>
    <p>
        <label><?php _e('What is the name of the ship in the TV show Firefly?') ?><br />
        <input type="text" name="user_proof" id="user_proof" class="input" size="25" tabindex="20" /></label>
    </p>
<?php }

add_action( 'register_post', 'add_register_field_validate', 10, 3 );
function add_register_field_validate( $sanitized_user_login, $user_email, $errors) {
    if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) {
        return $errors->add( 'proofempty', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question.'  );
    } elseif ( strtolower( $_POST[ 'user_proof' ] ) != 'serenity' ) {
        return $errors->add( 'prooffail', '<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.'  );
    }
}
This adds an extra box to the register screen with the question “What is the name of the ship in the TV show Firefly?” The user-provided answer is then compared to the official answer. If correct, the user is registered, otherwise WordPress-friendly error(s) are returned.
Advertisements

2 thoughts on “Add A Security Question to the Register Form

  1. Excellent! Working fine.
    Do you know how to add math values randomly and get it’s sum and validate it?
    Ex: – What is the 10 + 4 ? (this is random values)

    How can I validate this ?

    Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s