WordPress sidebar login form hack

April 14, 2005 @ 89 Comments

If you’re a regular visitor you’ve probably noticed the nice login form in the sidebar, which if you happen to login, will show your user name and user controls. It should work with most themes with little or no tweaking. Enjoy!

Update: This hack is no longer necessary; you can now use a beautiful widget to add the login form to your sidebar.

Update: Level 0 users always get redirected to /wp-admin/profile.php. There’s nothing I can do about this since it’s hard-coded into WordPress. Level 1 and above will return to the same page they logged in from. There is a bug open for this. Also on logout, you will always be redirected to /wp-login.php. There is a bug open for this as well.

Update 15 April: XeroCool has posted a modification for the login form for the Blix theme. I’m not sure if it works, since his own form seems to be a little misaligned. Unfortunately, even in 2005, we still can’t just write perfect XHTML and CSS and expect all browsers to render it properly. I’ll continue to keep you updated, and watch the comments section!

Update 21 April: Mark Jaquith has updated the two bugs above with patches which fix those issues, so hopefully these will be resolved in time for WordPress 1.5.1. I have also updated the code below to work with the patched WP code.

Update 25 June: I have updated the tabindex so as not to conflict with the WordPress default themes.

 <li id="login">
<?php
  global $user_ID, $user_identity;
  get_currentuserinfo();
  if (!$user_ID):
?>
  <h2><?php _e('Login'); ?></h2>
    <form name="loginform" id="loginform" action="<?php echo get_settings('siteurl'); ?>/wp-login.php" method="post">
    <div><label><?php _e('Login') ?>:<br /><input type="text" name="log" id="log" value="" size="20" tabindex="7" /></label><br />
    <label><?php _e('Password') ?>:<br /> <input type="password" name="pwd" id="pwd" value="" size="20" tabindex="8" /></label><br />
    <label><input type="checkbox" name="rememberme" value="forever" tabindex="9" /> <?php _e("Remember me"); ?></label><br />
    <input type="submit" name="submit" value="<?php _e('Login'); ?> &raquo;" tabindex="10" />
    <?php wp_register('', ''); ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>"/></div>
    </form>
<?php
  else:
?>
  <h2><?php echo $user_identity; ?></h2>
    <ul>
    <?php wp_register(); ?>
    <li><a href="<?php echo get_settings('siteurl') . '/wp-login.php?action=logout&amp;redirect_to=' . $_SERVER['REQUEST_URI']; ?>"><?php _e('Logout'); ?></a></li>
    </ul>
<?php
  endif;
?>
 </li>

89 Comments → “WordPress sidebar login form hack”


  1. Ross

    Apr 14, 2005

    Nice one! It works just instantly! :)

    Got it online on my site now :D

    Thanks again!


  2. M. Jones

    Apr 14, 2005

    Works absolutely awesomely! I’m definitely stealing this for my site. :D


  3. XeroCool

    Apr 15, 2005

    ioerror, Can you show a screenshot and what resolution and browser? It seem to work fine in FireFox and IE. Maybe CSS might help, but I need to know the prob or see a screenshot.


  4. Michael Hampton

    Apr 15, 2005

    XeroCool, I’ve e-mailed you the screenshot you asked for. I hope it helps in figuring out what’s going on. Drop by on IRC if you need any live assistance.

  5. Apr 15, 2005


  6. Lord Anki

    Apr 15, 2005

    There is one small error with the Journalized series with this hack.

    /wp-login.php" method="post">
    :
    :
    »" tabindex="3" />

    ">

    Is what I used. The echo user_id made that happen way out in the middle of the page in 800*600 browser.


  7. Jess

    Apr 16, 2005

    Perfect – thank you!


  8. Andreas Viklund

    Apr 16, 2005

    I’ve made it work with the Blix theme (or, sort of, I’ve made a total remake of the theme but I got the same error that XeroCool had. Take a look at my site, and let me know if you want the “minimized edit” that I have… (really, I’ve just removed code, and now it works for me).


  9. William

    May 03, 2005

    Hi

    You have a small problem here:

    ‘/wp-login.php?action=logout&redirect_to=’ . $_SERVER['REQUEST_URI']; ?>”> because it doesn’t close the link. It needs a closing </a>.

    I am using this for my site, a bit modified.

    Kind regards.


  10. Michael Hampton

    May 04, 2005

    William, you’re quite right. The </a> was dropped in error the last time I pasted the code in. I’ve added it back. Thanks for letting me know!


  11. Lelia Katherine Thomas

    Jun 05, 2005

    This is a great thing to have on a site, but the XHTML does not validate for me when I just paste what you have here. Yet, when I check the XHTML validation of your site, it doesn’t come up with the same errors. I’m curious: are you perhaps using a different version for your own sidebar?

    The problem is with one part of the code.

    '/wp-login.php?action=logout&redirect_to='

    Can I just use the & amp ; code here? I don’t know enough about PHP yet to know. >_


  12. Michael Hampton

    Jun 05, 2005

    I ran your site through the validator, and you have quite a few problems, but this is not caused by the sidebar login form. It is caused by the fact that you have <div id=”sidebar”> appearing twice (i.e. the first error on the validator’s response). Fix that and most of the rest should go away.


  13. Lelia Katherine Thomas

    Jun 05, 2005

    I know this. *laughs* That would be why I’m currently going through it all. I’m switching several files right now, too, so had I known you were actually online right now, I probably would have refrained from posting the message. ;) :p Anyway, I located the error. :) Sorry for the inconvenience!


  14. Usayd

    Jun 08, 2005

    I was wondering if this could be further improved to have the (once logged in), users avatar where it says logout and admin. That would be really cool :)


  15. Dan

    Jun 25, 2005

    Hey guys, sounds nice… but I can’t find a download link dor the sidebar login!


  16. Kaysha

    Jul 08, 2005

    Woah!!!
    Did great on my website…
    Thanx a million


  17. Adnihilo

    Jul 22, 2005

    Sounds just like I was looking for, but like Don of number 18 above, I can’t find the sidebar login hack script either. Is it a Rove double secret sort of hack?


  18. Michael Hampton

    Jul 22, 2005

    You’re kidding, right? It’s very hard to miss.


  19. Adnihilo

    Jul 29, 2005

    re. You’re kidding, right? It’s very hard to miss.

    Well no IO Error.. I can only assume that’s it under ‘Update 25 June ‘ starting with But what file does the hack go in. Not like I’m unfamiliar with php, but am a newbie to wordpress… Prior thx if you could clear that up for me – it’s is a nice hack..


  20. Adnihilo

    Jul 29, 2005

    Does the hack go in sidebar.php???


  21. Adnihilo

    Jul 29, 2005

    I got it – If only I had a brain as the Scarecrow says.. thx though for the hack IO Error!!


  22. Richard Brown

    Aug 09, 2005

    Simplicity and brilliance – well thought out and an excellent mod!

    I’ll buy you a beer sometime.

    Rich


  23. Steve

    Sep 29, 2005

    You rock – although you know that already J ..


  24. Vincent

    Oct 08, 2005

    Thanks excellent mod!

    Do you also have a WordPress mod for the sidebar where you can see how many visitors and how many members are currently on the weblog? I can’t find one.


  25. Arnaud

    Nov 23, 2005

    This is a great mod. Even with my non-knowledge of PHP I got this running. Thanks for this.

    Just one question. When I log-out it redirects me to the wp-admin.php. Is it possible to change this to my homepage?

    Thanks in advance!


  26. Karen

    Nov 28, 2005

    Any way to add a link for “Forgot Password” Next to the “Register” bit?


  27. Michael Hampton

    Nov 28, 2005

    Arnaud, go scroll to the top of the page and read the post.

    Karen, I suppose you could add a “Forgot Password” link. It would be rather long, though, and make the form kind of crowded.

  28. Dec 20, 2005


  29. bob

    Dec 31, 2005

    sorry but I haven’t a clue how to install this hack…

    My wp installation is members only behind a firewall – intranet employees only if you get the picture. I want to enable anyone to create a post with the idea of using posts as tasks that all employees can comment on.

    Is there anyway to remove the auth requirements or enable anonymous posting? Also once that is accomplished is it possible to provide a minimalist screen for posting without showing all the other admin links?

    thank


  30. MilleSabords

    Jan 23, 2006

    I’ve pasted the code to my sidebar.php theme, but i don’t get the register link
    http://millesabords.info/wp2bas/


  31. MilleSabords

    Jan 23, 2006

    Sorry, i only have to enabled visitor to register


  32. Sidd

    Jan 30, 2006

    Super cool !!! I have used it in my site :D
    http://www.raasmzone.com

    Thanks for sharing


  33. winston

    Mar 09, 2006

    help! i can’t figure out how to add a login box

  34. Mar 10, 2006


  35. Aki

    Mar 11, 2006

    This is only for the sidebar within the wordpress folder, If i place it in outside of WP, it will login BUT will not display user info and logout

  36. Mar 27, 2006


  37. deltanine

    Mar 30, 2006

    thanks a lot, exactly what i was looking for …


  38. hdtv freak

    Apr 05, 2006

    Thanks for the great code! I just have one question. Is it possible to use the “remember me” check box, or will this only work with the default WordPress login page?


  39. Michael Hampton

    Apr 05, 2006

    The “Remember me” feature is new to WordPress 2.0, and I haven’t gotten around to integrating it yet.


  40. Brian

    Apr 11, 2006

    hello, can anyone provide me a different link to the blix modification? the xerocool link seems to be down?

    thank you very much


  41. DKD

    May 08, 2006

    I am trying to get a login page to come up before users can access the main “Word press” blog page. I tried copying the above code into the index.php file but with no luck…please help.


  42. Carrie

    May 16, 2006

    Thanks!!

  43. May 29, 2006


  44. iBrev

    Jul 20, 2006

    Awesome! Great work!


  45. Tada

    Jul 30, 2006

    Does anyone still have the modification for Blix theme? the download link for it is down. Thanks!

  46. Works like a charm! Thank you!


  47. Umut Dogan

    Sep 25, 2006

    hello. i put this useful plugin to my site: umutdogan.com.
    But i couldn’t change text size as my other sidebar elements.
    i tried but it didn’t work.

    thanks ;)


  48. sfong15

    Oct 24, 2006

    Great script, I have actually used it for quite a while now and felt the need to simplify it a bit for my own use hence a plugin. See my post describing it and visit the site to see it on the sidebar.

    I’ll be trying to bring the lost password recovering into the sidebar as well without going to the default WP page.

    It would be perfect if there are geeks out there who can brush up the plugin with Ajax tricks such that logging in/out without refreshing of the page. This shouldn’t be too difficult should it? (to me something yet to be learnt!).

    Thanks again for the original scripts.


  49. silverelf

    Dec 12, 2006

    Is it possible to display the Write, Manage, Profile link in between the site admin and log out link after users logging in? Cause it would be more convenient to click Write link on the main page rather than going to Site Admin=>Dashboard=>Write =)


  50. John

    Dec 18, 2006

    Anyone know how to make this work with the sidebar widget?


  51. Mark

    Jan 14, 2007

    Hi,

    Is it still not possible to override redircting to the ugly wordpress login page when you enter the wrong login password? Logging in and out work great, my users are able to stay on my site.

    However they enter the wrong password/username they get redirected to that god awful page.

    Thanks


  52. Will

    Jan 17, 2007

    I hear you! I am looking for the same thing.

    I don’t know enough about php to know how to fix this but is there no way to redirect to another page/link if the wrong pass is entered?

    BTW – This is a great plugin Michael!

  53. Jan 21, 2007


  54. Josh

    Jan 27, 2007

    This is awesome!! Thanks!


  55. crizatu

    Feb 03, 2007

    Works great for my site! Thank you so much!


  56. Sam

    Feb 16, 2007

    Hey, I installed the script w a little size mod, works great. I also added a “Lost password?” link and moved “Register” a line down to make it align better. Here are screenshots of the and of the login.


  57. Sam

    Feb 16, 2007

    OK so I messed that one up a little, you get the picture though.


  58. Melissa Odom

    Feb 24, 2007

    How do I edit the look of the plugin. I have a big white space that does not go with the background. I am new to all this so please be kind. :)


  59. Melissa Odom

    Feb 24, 2007

    Nevermind….I got it!!! :) Thanks for the plugin.


  60. sesom

    Mar 03, 2007

    Thanx alot for this!

  61. Mar 07, 2007


  62. Raymond

    Mar 07, 2007

    Umm, seriously, where’s the download link?


  63. Stevenki

    Mar 09, 2007

    I can’t see a download link for this… Maybe it needs making a little less… stupid?


  64. Michael Hampton

    Mar 09, 2007

    There is no download link. Why are you asking for a download link?


  65. Stevenki

    Mar 10, 2007

    So its just the code above..? That wasn’t clear as others refer to it as a ‘plugin’ . Where does one paste the code to? I’ve tried it in sidebar.php and it doesn’t work there… I need installation instructions. Sorry if I’m missing the obvious.


  66. domkrats

    Apr 05, 2007

    Nice! Thanks for this! But I am facing one problem as i am using frames in my page! This feature works perfect in Firefox, but when i open IExplorer, this only works if i open just blog page (), but if i open which opens blog in frame, then it somehow doesn’t login – it just refreshes the frame and thats all!? Any ides why? Thanks!


  67. fluke

    Apr 07, 2007

    Thanks a bunch for the code I really like it! I had to modify it a bit to get it right for me but that wasn’t hard at all xD

    I have one question, I have a * next to the login panel that I can’t seem to get rid of. I can’t see it in the code at all. Here is what I mean -

    Just wondered if anyone else had that problem or if they knew how to get rid of it.


  68. krzysztof114

    Apr 12, 2007

    Hi fluke! You need to delete tags or edit your css file.

    And now my little problem:
    Everything is ok in FF and in Opera but IE tells me that something is wrong. The main page is shown just below the line when the login panel ends. I tried to fix it out by removing tags, but is doesn’t work.
    (Sorry for my Polish-English:P)
    Any ideas?


  69. silverelf

    Apr 13, 2007

    hey I had this problem too.. There’s something wrong with Michael Hampton’s code when displayed on IE and I posted for help in wordpress forum, another user sfong15 recommend me to use this instead, and it worked really well yupps try it out..


  70. Kevin

    Apr 22, 2007

    How do you make it so when you login, it takes you to “your” dashboard ..


  71. Michael Hampton

    Apr 22, 2007

    If you want to go to your dashboard when you login, remove this hack and restore the original code. :)


  72. Flash Buddy

    Apr 25, 2007

    Love this hack, just had to extend my kudos. Notice I called it a hack? That is the correct terminology isn’t it? It is not a plugin, right? I just want to show proper etiquette.

    This is every bit as cool as the SexyComments plugin by BorkWeb:
    http://borkweb.com/story/wordpress-plugin-sexy-comments

    Some of your users should have their doctor check for brain lesions. I’ve had some problems with those recently. After pasting this in to my sidebar (Senorita theme) I am presented with a link to go to my Dashboard if I so choose, or logout, nice!

  73. May 25, 2007

  74. May 28, 2007


  75. Tyler

    Jun 10, 2007

    Hi I was wondering if there’s any way to add this to comments.php… what I’d really like is a login right there so when my blog contributors want to post a comment but haven’t logged in already, they can just do it right there.

    I put it in the comments.php and it looked look and I thought it would work, but when I press login it tries to post a comment and I get an error saying “Please type a comment”. When I typed a comment and tried it, it posted an anonymous comment and didn’t log me in. Must be some sort of conflict or confusion with the comments.php code, I guess. I’d appreciate any help, because I’m new to php and winging it.


  76. Tim

    Jun 14, 2007

    A marvellously simple fix… thanks for sharing…


  77. Andrey

    Jun 28, 2007

    I love this hack !!! Good job …


  78. Tome

    Jul 01, 2007

    Yes, this sidebar login hack is GREAT! Like the posters above I would love to see

    1. Ajax login/logout feature.
    2. Way to add this login/logout feature in the commments section, of course, with Ajax incorporated.

    Thanks!


  79. alias

    Aug 10, 2007

    Is there anyway to use this without enabling register ? I notice when register is turned off the login form doesn’t appear, is there another way to set it up so it doesn’t require register to be enabled ?


  80. yli

    Aug 29, 2007

    hi… it is working but i’m using this in a MUwordpress… what i must to do to a redirection to the user dashboard after login. thanks


  81. Ruriko

    Dec 23, 2007

    How can I make the login form in a horizontal style?


  82. n00b

    Dec 28, 2007

    Easy. You just remove the little “”‘s where they shouldn’t be. That’s also pretty easy to figure out


  83. n00b

    Dec 28, 2007

    Oops. It removed the html tags.


  84. n00b

    Dec 28, 2007


  85. n00b

    Dec 28, 2007

    in other words,
    little-arrow-pointing-to-the-left,
    B R
    little-arrow-pointing-to-the-right.
    There.


  86. xyhzc

    Dec 31, 2007

    I find an issue, when you open a new IE window and login the first time, it will say cookie problem, but the second time will be ok, this only happens when you open a new IE window and login the first time, could you fix this problem? thanks!


  87. Michael Hampton

    Dec 31, 2007

    I can’t fix IE. Only Microsoft can do that, and they don’t seem to be inclined to actually fix things.


  88. Illy

    Jan 01, 2008

    Nice one just what i needed thanks a lot!


  89. vampire diaries

    Jan 03, 2012

    i use to use this hack so long ago. you are right no one can fix IE, most developers have fully given up on the older versions because it is such a pain


Leave a Reply

Copyright © 2012 Homeland Stupidity.

Bad Behavior has blocked 2527 access attempts in the last 7 days.