Create Custom Register Page In WordPress

how-to-create-wordpress-user-registration-form

Here we will explain to you how to create a custom register page in WordPress. this tutorial will help for making the custom front end page where your subscriber and author and many roles users will register them self.

Step 1)

Make One template page in WordPress active theme folder and put the below code there.

<?php
/*  Template Name: Register Template */
get_header();

function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

$error= '';
$success = '';
global $wpdb, $PasswordHash, $current_user, $user_ID;
if(isset($_POST['task']) && $_POST['task'] == 'register' ) {

	$password1 = randomPassword();
	$first_name = $wpdb->escape(trim($_POST['first_name']));
	$last_name = $wpdb->escape(trim($_POST['last_name']));
	$email = $wpdb->escape(trim($_POST['email']));
	$username = $wpdb->escape(trim($_POST['username']));
	
	if( $email == "" || $username == "" || $first_name == "" || $last_name == "") {
		$error= 'Please don\'t leave the required fields.';
	} else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
		$error= 'Invalid email address.';
	} else if(email_exists($email) ) {
		$error= 'Email already exist.';
	} else {
		$user_id = wp_insert_user( array ('first_name' => apply_filters('pre_user_first_name', $first_name), 'last_name' => apply_filters('pre_user_last_name', $last_name), 'user_pass' => apply_filters('pre_user_user_pass', $password1), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'subscriber' ) );

		if( is_wp_error($user_id) ) {
			$error= 'Error on user creation.';
		} else {
			do_action('user_register', $user_id);
			$success = 'You\'re successfully register';

		}
	}
}
?>
<div class="wrapper">
 	<div class="choko_register_box">
		<form method="post" class="choko_register_form">
			<div class="choko_register_row_ctn">
				<label for="first_name">First Name<span>*</span>:</label>
				<input type="text" value="<?php if(isset($first_name)){echo $first_name;} ?>" name="first_name" id="first_name" />
			</div>
			<div class="choko_register_row_ctn">
				<label for="last_name">Last Name<span>*</span>:</label>
				<input type="text" value="<?php if(isset($last_name)){echo $last_name;} ?>" name="last_name" id="last_name" />
			</div>
			<div class="choko_register_row_ctn">
				<label for="username">Username<span>*</span>:</label>
				<input type="text" value="<?php if(isset($username)){echo $username;} ?>" name="username" id="username" />
			</div>	
			<div class="choko_register_row_ctn">
				<label for="email">Email<span>*</span>:</label>
				<input type="text" value="<?php if(isset($email)){echo $email;} ?>" name="email" id="email" />
			</div>
			<div class="choko_register_msg_ctn">
				<?php if($sucess != "") { echo "<div class='choko_register_success_msg'>".$sucess."</div>"; } ?> 
				<?php if($error!= "") { echo "<div class='choko_register_error_msg'>".$error."</div>"; } ?>
			</div>		
			<button type="submit" name="btnregister" class="button" >Register</button>
			<input type="hidden" name="task" value="register" />
		</form>
 	</div>
</div>
<?php get_footer() ?>
<style>
	.choko_register_box{margin: 0 auto;width: 50%;}
	.choko_register_row_ctn{margin-bottom: 15px;}
	.choko_register_row_ctn label{display: block;margin-bottom: 10px;}
	.choko_register_row_ctn input{width: 97%;border-radius: 0}
	.choko_register_error_msg{color: red;margin-bottom: 15px;}
</style>

Step 2)

Now make register page in wp-admin/add new page. and select the “Register Template” as a template page. update your page.

Step 3)

Now visit your page in front end it will display the custom register form.

Leave a Reply

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

  +  65  =  73