Post Pic

Add a login form on your WordPress Theme

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!

Related Posts

Related Posts

No related posts.

25 Responses

Jul 20 2009 12:36

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

Jul 20 2009 23:16

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!

Jul 20 2009 23:21

by the way, the RT button doesn’t work on this page: -> http://tweetmeme.com/about/fail

Jul 21 2009 18:35

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-admin

you could use

/wp-admin

finally, i definitly agree with kaotoxin with the lowercase “login”…

BTW, nice and useful site, a real thanks for this,
KL

Jul 21 2009 18:39

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 ?

Jul 21 2009 22:44

nice, simple, useful! Thank you! I love this site!

Jul 23 2009 15:03

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.

Jul 23 2009 15:55

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.

Aug 01 2009 23:13

I’m getting a call to function error:

Call to undefined function current_user_can()

Can you help me?

Aug 01 2009 23:29

@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!

Aug 05 2009 00:09

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.

Aug 24 2009 19:11

that’s pretty good idea dude

Aug 27 2009 22:37

How would you modify the code to make it the wordpress registration form?

Sep 30 2009 23:02

Perfect post :)

You rock :P

Nov 17 2009 11:20

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

Dec 08 2009 17:36

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 ;) ….

Dec 09 2009 10:15

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?

Dec 23 2009 00:00

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.

Feb 17 2010 16:52

I’m looking for a code that is login-logout link only without the form.. can you recommend a site for me??

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required