This website uses cookies to allow us to see how the site is used. If you continue to use this site, we assume that you are okay with this. If you want to use the sites without cookies, please see our privacy policy.

Create your own registration form in WordPress

From time to time you might need to create your own registration form in WordPress to maintain a brand identity for your client. Although personally I’d be proud to show off the fact that I’m using WordPress, some clients just don’t and they would like their website to reflect their own branding so here’s how we do it.

You can start by putting this form anywhere in one of your theme files but, just to keep things clean I’m going to put this in a new template based on the page.php file so, first of all lets create a new template by duplicating the page.php file and call it something obvious, I’m going to call mine customer-registration.php. Then at the top add the standard template naming comment so you can select the template in the backend of your WordPress installation.

The code at the top of my new page customer-registration.php should look like this:

   Template Name: Registration Page

Next we’ll create the form but, first we need to add an action to hook on too for processing the form as we will be posting it to itself for demonstration purposes, somewhere above the form should be fine.

Then create a simple form with a method of “POST”, an id “adduser”, an empty action so we can post the form to itself and finally some inputs for a Username and Email address. I’ve also included php $error variables for some error checking and you’ll also notice the wp_nonce_field() for extra security after the submit input.

The form code should look something like this:

<?php do_action ('process_customer_registration_form'); ?><!-- the hook to use to process the form -->

<form method="POST" id="adduser" class="user-forms" action="">
	<p class="form-username">
		<label for="user_name"><?php echo 'Username (required)'; ?></label>
		<input class="text-input" name="user_name" type="text" id="user_name" value="" />
	<p class="form-email">
		<label for="email"><?php echo 'E-mail (required)'; ?></label>
		<input class="text-input" name="email" type="text" id="email" value="" />
	<p class="form-submit">
		<input name="adduser" type="submit" id="addusersub" class="submit button" value="Register" />
		<?php wp_nonce_field( 'add-user', 'add-nonce' ) ?><!-- a little security to process on submission -->
		<input name="action" type="hidden" id="action" value="adduser" />

Next we’ll need the action function to hook the do_action(‘process_customer_registration_form’) we created at the top of our registration form.

So in your functions.php file add the following code:

function registration_process_hook() {
	if (isset($_POST['adduser']) && isset($_POST['add-nonce']) && wp_verify_nonce($_POST['add-nonce'], 'add-user')) {
		// die if the nonce fails
		if ( !wp_verify_nonce($_POST['add-nonce'],'add-user') ) {
			wp_die('Sorry! That was secure, guess you\'re cheatin huh!');
		} else {
			// auto generate a password
			$user_pass = wp_generate_password();
			// setup new user
			$userdata = array(
				'user_pass' => $user_pass,
				'user_login' => esc_attr( $_POST['user_name'] ),
				'user_email' => esc_attr( $_POST['email'] ),
				'role' => get_option( 'default_role' ),
			// setup some error checks
			if ( !$userdata['user_login'] )
				$error = 'A username is required for registration.';
			elseif ( username_exists($userdata['user_login']) )
				$error = 'Sorry, that username already exists!';
			elseif ( !is_email($userdata['user_email'], true) )
				$error = 'You must enter a valid email address.';
			elseif ( email_exists($userdata['user_email']) )
				$error = 'Sorry, that email address is already used!';
			// setup new users and send notification
				$new_user = wp_insert_user( $userdata );
				wp_new_user_notification($new_user, $user_pass);
	if ( $new_user ) : ?>

	<p class="alert"><!-- create and alert message to show successful registration -->
		$user = get_user_by('id',$new_user);
		echo 'Thank you for registering ' . $user->user_login;
		echo '<br/>Please check your email address. That\'s where you\'ll recieve your login password.<br/> (Be sure to check your spam folder)';
	<?php else : ?>
		<?php if ( $error ) : ?>
			<p class="error"><!-- echo errors if users fails -->
				<?php echo $error; ?>
		<?php endif; ?>
	<?php endif;

add_action('process_customer_registration_form', 'registration_process_hook');

In the function above all we are basically doing is taking the form information and creating a new user from the information given by the user with an auto generated password and sending an email to that user with their login details, we are also making some error checks to make sure the user is created correctly, if there are no errors the user is created with a message telling the new user what to do next, if there are errors we tell the user what has happened so they can try again.

Now you should have a viable registration form that can be placed anywhere on your WordPress installation.


A geek who likes to code and be nice ^_^

2 thoughts on “Create your own registration form in WordPress

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.