Create custom wordpress registration page
I am sure, most web masters using wordpress do not like to let their users to signup or login through the boring wordpress default signup/login screens. Everyone would like to make those pages look like same as part of their website. Today we will have a little play with creating a WordPress custom registration page template. You may not forgot my last tutorial about creating a custom wordpress login page template as part of the theme. If you hadn’t read it yet, read it here Custom WordPress Login without using a plugin
Ok, now we will move in to our today’s tutorial.

Step1: Create page custom-register.php
First we will create a new php template file called custom-register.php and place it inside your wordpress theme folder your-domain-name/wp-content/themes/your-theme-name
Step2: Naming the Template file
< ?php /* Template Name: Custom WordPress Registration */ ?>
Step3: check if the user is not logged in
Like we did for custom login, we must first check whether the current user is logged in or not. We will show the registration form only if the current user is not logged in. Additionally, we need to include the file registration.php from wp-includes folder in order to create a new user inside wordpress system.
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
if (!$user_ID) {
//All code goes in here.
}
else {
wp_redirect( home_url() ); exit;
}
Step4: Embedding the Register Form and jQuery Ajax
Before we display the register form we need to check whether the user registration is allowed by the administrator using the function get_option('users_can_register').
<?php
if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator
?>
<?php the_title(); ?>
<div id="result"></div> <!-- To hold validation results -->
<form action="" method="post">
<label>Username</label>
<input type="text" name="username" class="text" value="" /><br />
<label>Email address</label>
<input type="text" name="email" class="text" value="" /> <br />
<input type="submit" id="submitbtn" name="submit" value="SignUp" />
</form>
<script type="text/javascript">
//<![CDATA[
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url: "",
data: input_data,
success: function(msg){
$('.loader').remove();
$('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
//]]>
</script>
<?php
}
else echo "Registration is currently disabled. Please try again later.";
?>
Step5: Validate the inputs and register the user
Add the following php code inside if (!$user_ID) { } and move the register form inside the else part of the following if condition.
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST['username']);
if(empty($username)) {
echo "User name should not be empty.";
exit();
}
$email = $wpdb->escape($_REQUEST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
echo "Please enter a valid email.";
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo "Username already exists. Please try another one.";
else {
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Registration successful";
$msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
wp_mail( $email, $subject, $msg, $headers );
echo "Please check your email for login details.";
}
exit();
}
else
{
//Embed the register form and javascript here
}
In the above code, we will validate the inputs and then create random password with the use of function wp_generate_password(). With the use of wp_create_user function create a new user account if not the username already exists in the system. Finally, we will email the login details including the random generated password to the signed up user.
Full Code Preview
< ?php
/*
Template Name: Custom WordPress Signup Page
*/
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
//Check whether the user is already logged in
if (!$user_ID) {
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST['username']);
if(empty($username)) {
echo "User name should not be empty.";
exit();
}
$email = $wpdb->escape($_REQUEST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
echo "Please enter a valid email.";
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo "Username already exists. Please try another one.";
else {
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Registration successful";
$msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
wp_mail( $email, $subject, $msg, $headers );
echo "Please check your email for login details.";
}
exit();
} else {
get_header();
?>
<!-- <script src="http://code.jquery.com/jquery-1.4.4.js"></script> -->
<!-- Remove the comments if you are not using jQuery already in your theme -->
<div id="container">
<div id="content">
<?php if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator ?>
<?php the_title(); ?>
<div id="result"></div> <!-- To hold validation results -->
<form action="" method="post">
<label>Username</label>
<input type="text" name="username" class="text" value="" /><br />
<label>Email address</label>
<input type="text" name="email" class="text" value="" /> <br />
<input type="submit" id="submitbtn" name="submit" value="SignUp" />
</form>
<script type="text/javascript">
//<![CDATA[
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url: "",
data: input_data,
success: function(msg){
$('.loader').remove();
$('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
//]]>
</script>
<?php } else echo "Registration is currently disabled. Please try again later."; ?>
</div>
</div>
<?php
get_footer();
} //end of if($_post)
}
else {
wp_redirect( home_url() ); exit;
}
?>
Once the template file is ready, you need to create a new page from the wordpress admin. If you are unsure about this just follow the step number 10 from my previous tutorial Custom WordPress Login without using a plugin. Hope you find this tutorial useful.










This is perfect. I love the site. The registration with all the extra fields, specially the password option and the confirmation message, the validation link e-mail, once logged in the optional edit profile page without the boring WP default page, the Welcome “user” at the top right, Edit profile/logout link….just want I want in my site…..can I have it???? Please…would appreciate a lot. Awesome stuff!!!!!!
I am glad you like it. I have already written tutorials similar to that feature. You can grab it from the tutorials page.
Great tutorial but you should bind listener to the form’s submit event, not submit buttons click because you can submit the form in other ways, not just by clicking submit button.
Using your listener method will not allow the user to submit on touch devices either like the iPhone or iPad.
jQuery(“#form-id”).submit(function() { … });
I didn’t think about it when writing this. Anyway thanks for the great tip.
Nice article.
Here you will find free plugins and lot of articles related to wordpress.
http://www.wordpressstop.com
Keep Smiling.
I’m a newbie in wordpress and I know that this is a stupid question. Just bear with me. What is the content of the registration.php?
I am not sure about which content you mean. The content of registration.php file should contain all code from full code preview. If you’re asking about page creation from wp admin, then just leave the content empty.
Will this work with multi-site installs?
i am not sure about the separate MU installation. But it will work for wordpress(3.0 and after) with network enabled.
Thanks will give it a go. I have the latest WP with network enabled using subfolder multisites.
nice tutorial, but can you also post how to create custom wordpress lost password form like the one tutorialstag is using…
thanks
Thank for this great tutorial.
Me too I’m asking for creating custom lost password page, just like the one you using in your blog. Thanks in advance.
nice post. glad you sorted the website.
Wow
1. This made my day! thanks
Did some of you also get this weird thing, where input fields are really long!?
For me it looks like this:
http://imageshack.us/f/190/screenshot20111118at710.png/
I tried centering the whole thing, but that didn’t do the job :/
Any suggestions!?
Hi Busk, Glad you like this tutorial. That should be a css problem, there may be some width applied to inputs as common in your css. Try adding separate width to your form fields: if you have the class name for the inputs as ‘text’ then paste this in your css
input.text { width: 200px }
PERFECT that did the job
thank you very very much!
Tried it and it didn’t work. All I got was a plain text page with
<?pgp /*Template Name: Custom WordPress Login …….
I checked the permissions and everything, but it's not seeing the file as a php script…but as a text file.
Any simple fix for this?
thanks
Found the issue. All of your “< ?php" has a space between the "<" and "?php". Got rid of the space and it worked….kinda.
Now I'm getting a "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'….on line 97
there is some issues with the code previewer. You can download the source file and try again.
Hi there!! Thanks for the update. I’ll test it out soon.
I just started using s2member to restrict the downloads to registered members. It’s working out really well and they have their own sign-on and register. Now, can I bypass theirs to use your custom ones?
thanks
Membership plugins registration involves different user level, you need to customize my code in order to support different user levels. If you are using s2member only to restrict downloads to registered members (irrespective of user levels), just a file download plugin is enough.
Hi
Thanks for the tutorial, it is great to have our own login and registration page.
However, I am trying to do the same with a new template “Lost password” in case user do not rememner the password or the username to login, however it is quite more complex as it requires:
- Reset password of user / email
- Send a mail notifying user the password has been changed with one of these possible options:
opcion 1: send new password
option 2: send a link where user can change the password
Any ideas, our tutorials about how to create this template?
Many thanks
Joan
Thanks for the suggestion. I am getting many emails regarding that. Soon you can expect a tutorial about it.
will try this code and let you know,,,Thanks for ur tutorials…
Hi Hannan. Please let me know how can I redirect user to home page after succesful registation. I tried wp_redirect( home_url() ); exit; after the wp mail function but no success. Thanks
FYI: My name is Kannan.
PHP redirection cannot be used in ajax call. You can use window.location in ajax success call.
i mean on line number 69
success: function(msg){ if(msg == "Please check your email for login details.") { window.location="<?php bloginfo( 'url' ); ?>"; } $('.loader').remove(); $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow'); }Thank you for this tutorial. Clean and simple and very well explained.
This did the trick perfectly without needing any plugins and it matches my site’s theme.
This and the other one (Custom Login) sava my life.
Thanks so much!!
Thanks for the very detailed tutorial…
Thanks for a great tutorial. It really helped a part of what I`m trying to do. The other part is auto login and redirect and I tried the above solution for the redirect but it did not work.
I would be more than grateful if you can help me make the user auto login upon registration and redirected to my url.
I`m using wordpress 3.3.1
Thanks!!
Great tutorial for those of you who may not be getting the jquery to work replace the “$” with “jQuery”. WordPress loads it’s jQuery library and my often cause conflict.
Hi,
Thanks for the tutorial. I noticed when the email sent out that is coming from the admin email but the name of the sender “WordPress” Where can I change that.
Thanks
Just change the $header variable in line 29 to
$headers = "From: YOURNAME <".$from . ">\r\n";
Don’t forget to redirect “wp-login.php” to your new registration page in your HTACCESS file.
Great tutorial but you should bind listener to the form’s submit event, not submit buttons click because you can submit the form in other ways, not just by clicking submit button.
Using your listener method will not allow the user to submit on touch devices either like the iPhone or iPad.
jQuery(“#form-id”).submit(function() { … });
Hi Kannan
Thanks for a great tutorial and registration page.
I am having a small problem which I have been unable to solve. The problem is some sort of a width problem, when the page loads the sidebar is pushed down the page. With the top of the sidebar starting at the bottom of the form. Any help will be appreciated
Thanks
AllanIT
Thanks for a great tutorial. How can i submit custom fields in data base. In wp_usermeta table.
This looks like exactly what I need, but when testing the form, the “results” div always gets populated with the home page content.
The URL in your code is a blank string for the ajax call, I assume that the page is supposed to submit back to itself and grab the error messages from the top of the page, but it does not seem to be doing that at all. Instead it just loads the home page into the “results” container div.
I even tried to put the url of the page in there but got the same result. Any help here would be great.
it means your form doesn’t submit as POST method as it is not coming in to if(post) section. Check the form method and inputs. You may have errors in your input fields.
Hi,
I have a special theme register.php file. I want to add additional space register.php file, but add-ons do not work. Because of this a special register file.
To manually add the additional space would be grateful if you write what to do. thanks.
This is great – just one question: How would I modify your script to not require email verification and just automatically verify the user once they fill out their username and password… and then have them automatically logged on once they complete the registration?
For me validation doesn’t work good, it shows errors in blank page after refresh not inline via javascript. jQuery is loaded. But newer version than in article.
Also you code have error like this id: #wp_signup_form
I don’t see any object in html with that ID.
help needed..
I have a sign up form added to the website. what I want is, when a user signs up he should be redirected to a membership form where first he purchase membership from PayPal through a buy now button and then redirected back to membership form and once he provide the details, all the data should be found in POST.