Custom WordPress Login without using a plugin
Today we are going to see how to make a Custom WordPress Login page template. You may seen a lot of plugins available out there to customize the wordpress default login page. But most of those plugin activated login page will not match your theme style and it offers very less customization options. So why don’t we create a custom wordpress login page template as part of our theme?
In this tutorial we will learn about making a WordPress Login page template without using any plugin but with the use of jQuery. Yes, we will use jQuery to show login error messages without page-refresh.

Step1: Create a new page called custom-login.php
The first step to do is create a new php template file called custom-login.php and place it inside your wordpress theme folder your-domain-name/wp-content/themes/your-theme-name
Step2: Naming the Template file
Place the following the code inside custom-login.php
< ?php /* Template Name: Custom WordPress Login */ ?>
First, we must check whether the current user is logged in or not. If the current user is not logged in, we will show the login form otherwise redirect the user to the homepage.
< ?php
global $user_ID;
if (!$user_ID) {
//embed the login form and validation code.
}
else {
echo "";
}
Step3: Call the theme header
All following codes should go inside the condition if (!$user_ID) { }.
get_header();
Step4: Page title function
the_title();
Step5: Embedding the Login Form
Step6: Including jQuery library
If you are not already using the jQuery library in your wordpress theme, you should add it now. Open up the header.php from your theme’s folder and place the following code just above “< / head>“.
Step8: PHP Validation
Add the following php code above get_header() and get the login form inside the else part of the following if condition. Also move the get_header function inside the else part.
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST['username']);
$password = $wpdb->escape($_REQUEST['password']);
$remember = $wpdb->escape($_REQUEST['rememberme']);
if($remember) $remember = "true";
else $remember = "false";
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = $remember;
$user_verify = wp_signon( $login_data, true );
if ( is_wp_error($user_verify) )
{
echo "Invalid username or password. Please try again!";
exit();
} else
{
echo "";
exit();
}
} else
{
//Embed the Login form here
}
I've been used the wordpress's wp_signon function to authenticate the user. wp_signon() is a wordpress function to authenticate users which accepts the user info parameters as an array.
And i have been defined my own error messages to show up on failed login attempt.
If you want the default wordpress error messages to show, just replace
echo "Invalid username or password. Please try again!";
to
echo $user_verify->get_error_message();
Step9: Call the footer function
Put this code just before the end of the else part.
get_footer();
Full Code Preview
< ?php
/*
Template Name: Custom WordPress Login
*/
global $user_ID;
if (!$user_ID) {
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_REQUEST['username']);
$password = $wpdb->escape($_REQUEST['password']);
$remember = $wpdb->escape($_REQUEST['rememberme']);
if($remember) $remember = "true";
else $remember = "false";
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = $remember;
$user_verify = wp_signon( $login_data, true );
if ( is_wp_error($user_verify) )
{
echo "Invalid username or password. Please try again!";
exit();
} else
{
echo "";
exit();
}
} else {
get_header();
?>
window.location='". get_bloginfo('url') ."'";
}
?>
So we have the template file ready. Next step is to make a new page from the wordpress admin area using the newly created page template.
Step10: Add new blank page
Login to your wordpress admin and navigate to "Add New Page". Write some title for the page and leave the content box empty. You can see our newly created page template "Custom WordPress Login" under the Template list. Select it and finally Publish.

That's it.













Hi Kannan,
This is a very nice tutorial! I really like it and quite easy to follow for WordPress noob like me.
I do appreciate your thoughts and times for putting it up.
Thanks,
Louis
Very useful tutorial to me. thank you.
I finally go this working its really easy to do, would like to do the same for the register user page as well. How could this be done.
Thanks for reading the tutorial. You can expect the registration part soon.
Could you tell me how I might redirect a user to page I created for them with their username?
You need to change the path in this, echo “window.location=’”. get_bloginfo(‘url’) .”/path-to-any-page’”;
inside if($_POST){ }.
If you want to print the logged in user’s data, then you could use
$user_info = get_userdata($user_ID);
echo “Username: “.$user_info->user_login; You can find more about get_userdata function here http://codex.wordpress.org/Function_Reference/get_userdata
This is really awesome! I’m glad I found this. Thanks for the help.
Hi Kannan,
Thanks for your great effort.
I did it as you said, but when I click on ‘view page’ in the admin area where the page list is, it is redirecting me to the home page instead of the custom-login page. Any idea what I have missed out?
Thanks in advance.
You should log out before testing the page. The page will redirtect to home page if the user is already logged in and i have mentioned it clearly in the step 2.
Hi there, this is only working for me at the root directory level. If I click on any links (which have pretty links) or the wp-admin I have to log in again.
Any ideas?
This is because sometimes the user info may not be populated after log in. Try using wp_set_current_user($user_verify->ID); after calling wp_signon() function.
Jack’s fix of changing true to false worked for me too. wp_set_current_user did not.
Hmm, I’ve fixed it by changing this:
$user_verify = wp_signon( $login_data, true );
To this:
$user_verify = wp_signon( $login_data, false );
Hi Kannan,
Great post thanks just what I needed!
One question I have is is it possible to send the logged in visitor to a page that is specific to the user id? I am building a client area on my site and want each client to have their own page.
Any suggestions?
Thanks for the great post.
David
This is not working for me. I still see the standard wordpress login page. When i replace the standard with the custom-login in the wordpress root folder it throws errors like “Call to undefined function get_header() in /Applications/XAMPP/xamppfiles/htdocs/kproduction/wp-login.php on line 37″
Help in getting this fix.
This will not replace the standard wordpress login page. This is a template file which should be a part of your theme. so you should not place this in the root but place it in your currently active theme folder and then create a new page from the admin area by selecting template style ‘Custom WordPress Login’. Then logout from admin and point to the new page created and that’s your alternative login page.
I forget how to tell what the username and password and remember me is in php.. what I mean is when you have this login and say you want admin for the username how to say that php so that is what the username is?
Can you explain your question clearly?
Also how do you redirect I got this part
You need to change the path in this, echo “window.location=’”. get_bloginfo(‘url’) .”/path-to-any-page’”;
inside if($_POST){ }.
If you want to print the logged in user’s data, then you could use
$user_info = get_userdata($user_ID);
echo “Username: “.$user_info->user_login; You can find more about get_userdata function
but it doesn’t make sense to me..
Also I tried this with my theme.. but it not working right..
For anyone who has experience losing their session after redirection or page refresh, just include this snippet before redirecting the user (line 29 under the Full Code Review section above)
apply_filters(‘login_redirect’, $redirect_to, isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ”, $user_verify);
I don’t what it really does, it is included in wp-login.php and it fixed my issue.
Thanks for this great tutorial! ^__^
hey, great tutorial!
but I’ve got some difficulties with javascript. the loader.gif doesn’t appear. the script after click doesn’t work at all. despite of that, form is sent (without ajax?)
r u sure that js you wrote is correct?
Did you included the jQuery or check for multiple inclusion of jQuery library. May be You may got some other js mistake.
I’m getting an error:
Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in ——– on line 25
line 25 is:
echo “Invalid username or password. Please try again!”;
I cut and pasted code.
Any idea why its not working.
Thanks
i have the same problem as you. send me a mail or let me know somehow. my email is tim_levinsson@hotmail.com
Hi, I think your info is very good and I will work hard at it, starting an online community and need help. This is all great, the problem I have is how to and where to put the buttons once this is created. I see many websites with register here, or log in here. So when all is said and created where do i put these files or links? in widget are of do i have to put these codes in the editor? thanks for your help.
lots and lots of errors.
Hi @Teniescu this tutorial is for intermediate developer if you can’t follow the tutorial provided above you can also visit my tutorial on how to create custom login page in wordpress.
Such a great post, I use this on my project thanks for such a good article.
Thanks!
thank you! this is the tutorial that I want. I will try this at home.
Thanks for this!
I love to see this with a Forgot Password link and the code for that form as well. Its something I could really do with.
Many thanks again!
Its published here http://www.tutorialstag.com/wordpress-custom-password-reset-page-template.html
very nice and creative …thanks dear
Hi, I am using your custom login code.
But after login, when i go to profile page, it gets redirected to login page with reauth.
My wordpress version in 3.3.1.
You may try Jack’s fix above
$user_verify = wp_signon( $login_data, false );
I am having the same problem where the login user information will not persist. The $current_user->ID exists but when redirected the value is 0?
wp_set_current_user( $current_user->ID );
wp_set_auth_cookie( $current_user->ID );
wp_safe_redirect(‘some_page.php’);
Please note I am attempting to do this via ajax and via a remote post.
Even basic attempts to set a cookie are failing.
Any help would be appreciated.
i have gotted the following error plz tell me how to solve:-
Parse error: syntax error, unexpected $end in E:\wamp\www\akhilesh_viscon\wordpress\wp-content\themes\theme_akhil\custom-login.php on line 81
and the code is:-
escape($_REQUEST['username']);
$password = $wpdb->escape($_REQUEST['password']);
$remember = $wpdb->escape($_REQUEST['rememberme']);
if($remember) $remember = “true”;
else $remember = “false”;
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = $remember;
$user_verify = wp_signon( $login_data, true );
if ( is_wp_error($user_verify) )
{
echo “Invalid username or password. Please try again!”;
exit();
} else
{
echo “window.location=’”. get_bloginfo(‘url’) .”‘”;
exit();
}
} else {
?>
Username
Password
Remember me
$(“#submitbtn”).click(function() {
$(‘#result’).html(‘<img src="/images/loader.gif” class=”loader” />’).fadeIn();
var input_data = $(‘#wp_login_form’).serialize();
$.ajax({
type: “POST”,
url: “”,
data: input_data,
success: function(msg){
$(‘.loader’).remove();
$(‘
‘).html(msg).appendTo(‘div#result’).hide().fadeIn(‘slow’);
}
});
return false;
});
This is exactly what I was looking for.
But im having a small problem. When the user loggin successfully, the page loads into the div id=”result” instead reloading it correctly as its supposed to be with the window.location.
How can I fix it?
My email is
j o h n m e r c e d @ g m a i l . c o m
Is there anyone that could help me with the question that I posted before?
Thanks
it does not appear form, it appears the home page, I expect an answer soon from you
I had to sift through the code and fix all the errors. Pretty sloppily written… but it works once all the quotes and spaces are fixed up.
Why not you download the source file? i added that for you guys to download.
Hey Kannan,
Thanks for the excellent post. Your file works like a charm for me. Could you help me navigate visitors who Log out to the homepage instead of the wordpress login screen.
You can use the below logout link on your page
<a href="<?php echo wp_logout_url( 'http://yourwebsite.com/customlogin/' ); ?>" title="Logout">Logout</a>hi kanan,
i try both your regi and login page on wp 3.3 but none of them work for me.
is it compatible with high vrsion
thanx
sanj
I tested on latest version 3.4.2, it is working good.
hi, great article but i have a question, remember me is not working, here after making remember me checkbox true, login box still not remembering username and password.!!
Hello,
Can you advise if you have any tutorial with a jQuery tabbed options.
For example I have a tab option for Register, Login and Forgot Password.
When I click the Register button the Login form fades out the Register form fades in and the same for Forgot password.
It will be a brillaint solution
Thanks
Regards
Hi
I would highly appreciate if somebody could give me directions how to modify and where exactly i need to put this code to have working in a sidebar on my wordpress site.
Thank you very much !
Awesome tutorial mate
Thanks
Nice custom login and combined with custom post type for the message
http://bisnisukm.com/wp-login.php
Thanks for this code!
May i ask if you are already login and then your going to your custom login page, it shows blank.. do you have code for this?
Anyway thanks again!
hello!,I like your writing so much! share we communicate extra approximately your article on AOL?
I need a specialist in this area to solve my
problem. May be that is you! Having a look ahead to see
you. pożyczki pod zastaw
Anyone wanting to do ensure error messages remain in the same view and don’t give you a white screen needs to follow these steps:
1. In your functions.php cold place (per Otto: http://wordpress.stackexchange.com/questions/41370/using-get-variables-in-the-url)
add_action(‘init’,'add_my_error’);
function add_my_error() {
global $wp;
$wp->add_query_var(‘my_error’);
}
2. In your template, replace the echo within if(is_wp_error($user_verify)) with:
echo “window.location=’”. add_query_arg( ‘my_error’, ‘invalid’ ) .”‘”;
3. Paste this wherever you want the error message to display:
<?php $error = get_query_var('my_error'); if ($error == 'invalid'){
echo "Invalid username or password. Please try again!”;
}
?>
I want a dropdown to choose user role also in the form,how can i do that?
it prints an error “Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in C:\wamp\www\wordpress\wp-content\themes\twentyeleven\Custom-login.php on line 25″
What should i do?
Code relevant to line 25 is-
if ( is_wp_error($user_verify) )
{
echo ” Invalid username or password, Please try again! ;”;
exit();
} else
{
echo “window.location=’”. get_bloginfo(‘url’) .”‘”;
exit();
}
}
I really like this blog and this tutorial too
thanks alot