
Are you using WordPress as a cms or as a community site? If yes, it can be a cool idea to display a login form in your blog sidebar or on a specific page. Here’s a simple code to do it.

Are you using WordPress as a cms or as a community site? If yes, it can be a cool idea to display a login form in your blog sidebar or on a specific page. Here’s a simple code to do it.
Nothing hard at all. Simply paste the following code where you'd like to display your login form. (For example, on your blog sidebar, or on a page template)
<<?php if (!(current_user_can('level_0'))){ ?>
<h2>Login</h2>
<form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" />
<input type="password" name="pwd" id="pwd" size="20" />
<input type="submit" name="submit" value="Send" class="button" />
<p>
<label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
</p>
</form>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a>
<?php } else { ?>
<h2>Logout</h2>
<a href="<?php echo wp_logout_url(urlencode($_SERVER['REQUEST_URI'])); ?>">logout</a><br />
<a href="http://XXX/wp-admin/">admin</a>
<?php }?>
Once saved, your theme will display a login form. Definitely easier and quicker for login!
28 Responses
Hi dude,
you have a pretty cool websites and very interesting posts.
but one thing i miss here. I would love an image which shows the effect of the mentioned code.
Maybe you can ad a function like this.
keep on writing! love your articles
Again, a very interesting and simple to get working tutorial, thank you very much!
By the way, just for “consistency” matters, may be you should use a lowercase title for all your tags (“L”ogin, “l”ogout) so people could style both the same way with a single CSS declaration.
Thought it would make a constructive comment.
Keep up the great work!
Thank you!
by the way, the RT button doesn’t work on this page: -> http://tweetmeme.com/about/fail
could be useful for new wordpress user, i’ve been using a similar code in many project,
just two correction :
line1, the double “<” is certainely a typo mistake, nothing important but cleaner
last line:
instead of
http://XXXX/wp-adminyou could use
/wp-adminfinally, i definitly agree with kaotoxin with the lowercase “login”…
BTW, nice and useful site, a real thanks for this,
KL
ahhh,
too bad it doesn’t take the code ,
so…
last line:
instead of
http://XXXX/wp-admin
you could use
?php bloginfo(‘url’) ? /wp-admin
don’t forget before and after the ?
nice, simple, useful! Thank you! I love this site!
Brilliant — I’m good enough with PHP to use the template tags for most things, but I couldn’t figure out how to do this. I would agree that a screenshot would be helpful, but not necessary. Can’t wait to use this! Thank you so much.
Thanks everyone for the comments!
I often thought about screenshots, but it will consume too much time, and WpRecipes concept is “raw” code snippets.
On my other blog Cats who code, you’ll find more “in-depth” tutorials.
I’m getting a call to function error:
Call to undefined function current_user_can()
Can you help me?
@Elizabeth: Very weird, I have used this code, including this function on a WP site a few weeks ago without any problem.
Maybe some help here and there.
Good luck!
Maybe this form shoud be hidden by default, but when you want to login, you can expand it, for example using the jQuery functions – hide/show triggered from header ot that block.
that’s pretty good idea dude
How would you modify the code to make it the wordpress registration form?
Perfect post
You rock
Thank you,
Just what I was looking for. WordPress is great because it is so customizable, and because of all the people, including you, making it easy.
Michael
Since WP 2.7 you can use this hook
… echo wp_login_url(get_permalink()) …
to refere to the loginpage
sorry for doubleposting but i lost my hook
….
Hi!
There’s a WordPress AJAX login sidebar widget that provides a login interface. I’ve also seen a couple of login screen branding plugins (I’ve also got a branding plugin of my own) that do a pretty good job if skinning the login page.
However, the one problem I’ve noticed is that we don’t have a filter for the login screen to make modifications to the existing login page screen elements (except by modifying the core WordPress files). For example, what if you do not want the “remember me” to be indefinite? Or if you want the “remember me” checked by default?
Anyway, these are just my thoughts. I wonder how other developers are solving the issue. Perhaps by redirecting to a custom login page? Or by overwriting the default login page via script based on settings?
Thanks for posting this tip. I am using WordPress with the WishList Member plugin to host a manual for a client, and this was just what I needed to add to the theme to make it easier to use.
I’m looking for a code that is login-logout link only without the form.. can you recommend a site for me??
Great piece of code, however, is something similar like this also possible to integrate the registration options / lost password into a page??
Hi, thanks for this. It’s just what I needed.
Question – is it possible to have it redirect to a front-end page, rather than the admin screen after login? I’m using this for member content in conjunction with the Members plugin by Justin Tadlock. I want the user to stay on the front end, if possible.
Any help would be appreciated!
Does anyone of you have any idea of how to replace the WordPress redirection during a login error?, I mean, I don’t want it to redirect me to the default WP window, I want to display an error message with PHP…
Trackbacks: