Add A Security Question to the Register Screen

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

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.

5 thoughts on “Add A Security Question to the Register Screen”

    1. The easiest way to use this code it to place it in a php file (don’t forget to add an opening <php tag) and put that php file in wp-content/mu-plugins. That directory isn’t created by default, so you may to create it yourself. However WordPress will automatically execute any php files in mu-plugins, so you don’t have to activate or don anything from there.

  1. This is exactly what i want to achieve for a user side form I’ve made, but the coding is obviously set up for the reg form. How do I change this to slot into a user form I’ve created using a page template?

  2. You should provide more info on your article. The way it’s written is like “I’m going to give some basic info, let them do the rest by themselves, I don’t care.”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: