WordPress sidebar login form hack

April 14, 2005 @ Michael Hampton103 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>

Article Series - WordPress Hacks

  1. WordPress sidebar login form hack
  2. WordPress search posts and pages hack
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.75 out of 5)
Loading ... Loading ...

103 Comments → “WordPress sidebar login form hack”


  1. M. Jones

    Apr 14, 2005

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

    Reply

  2. Ross

    Apr 14, 2005

    Nice one! It works just instantly! :)

    Got it online on my site now :D

    Thanks again!

    Reply
  3. Apr 15, 2005

    Reply

  4. 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.

    Reply

  5. 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.

    Reply
  6. Apr 15, 2005

    Reply

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

    Reply

  8. Jess

    Apr 16, 2005

    Perfect – thank you!

    Reply

  9. 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).

    Reply
  10. Apr 24, 2005

    Reply

  11. 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.

    Reply

  12. 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!

    Reply

  13. 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. >_

    Reply

  14. 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.

    Reply

  15. 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!

    Reply

  16. 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 :)

    Reply
  17. Jun 11, 2005

    Reply

  18. Dan

    Jun 25, 2005

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

    Reply

  19. Kaysha

    Jul 08, 2005

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

    Reply

  20. 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?

    Reply

  21. Michael Hampton

    Jul 22, 2005

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

    Reply

  22. 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..

    Reply

  23. Adnihilo

    Jul 29, 2005

    Does the hack go in sidebar.php???

    Reply

  24. Adnihilo

    Jul 29, 2005

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

    Reply

  25. Richard Brown

    Aug 09, 2005

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

    I’ll buy you a beer sometime.

    Rich

    Reply

  26. Steve

    Sep 29, 2005

    You rock – although you know that already J ..

    Reply

  27. 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.

    Reply
  28. Nov 15, 2005

    Reply
  29. Nov 21, 2005

    Reply

  30. 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!

    Reply

  31. Karen

    Nov 28, 2005

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

    Reply

  32. 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.

    Reply
  33. Dec 20, 2005

    Reply

  34. 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

    Reply

  35. 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/

    Reply

  36. MilleSabords

    Jan 23, 2006

    Sorry, i only have to enabled visitor to register

    Reply

  37. Sidd

    Jan 30, 2006

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

    Thanks for sharing

    Reply

  38. winston

    Mar 09, 2006

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

    Reply
  39. Mar 10, 2006

    Reply

  40. 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

    Reply
  41. Mar 27, 2006

    Reply

  42. deltanine

    Mar 30, 2006

    thanks a lot, exactly what i was looking for …

    Reply

  43. 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?

    Reply

  44. 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.

    Reply

  45. 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

    Reply

  46. 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.

    Reply

  47. Carrie

    May 16, 2006

    Thanks!!

    Reply
  48. May 29, 2006

    Reply

  49. iBrev

    Jul 20, 2006

    Awesome! Great work!

    Reply

  50. Tada

    Jul 30, 2006

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

    Reply
  51. Works like a charm! Thank you!

    Reply

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

    Reply
  53. Sep 27, 2006

    Reply
  54. Oct 14, 2006

    Reply

  55. 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.

    Reply
  56. Oct 24, 2006

    Reply
  57. Oct 25, 2006

    Reply
  58. Nov 20, 2006

    Reply

  59. 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 =)

    Reply

  60. John

    Dec 18, 2006

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

    Reply

  61. 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

    Reply

  62. 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!

    Reply
  63. Jan 21, 2007

    Reply

  64. Josh

    Jan 27, 2007

    This is awesome!! Thanks!

    Reply

  65. crizatu

    Feb 03, 2007

    Works great for my site! Thank you so much!

    Reply

  66. 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.

    Reply

  67. Sam

    Feb 16, 2007

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

    Reply

  68. 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. :)

    Reply

  69. Melissa Odom

    Feb 24, 2007

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

    Reply

  70. sesom

    Mar 03, 2007

    Thanx alot for this!

    Reply
  71. Mar 07, 2007

    Reply

  72. Raymond

    Mar 07, 2007

    Umm, seriously, where’s the download link?

    Reply

  73. Stevenki

    Mar 09, 2007

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

    Reply

  74. Michael Hampton

    Mar 09, 2007

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

    Reply

  75. 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.

    Reply
  76. Mar 26, 2007

    Reply
  77. Mar 27, 2007

    Reply

  78. 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!

    Reply

  79. 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.

    Reply

  80. 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?

    Reply

  81. 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..

    Reply

  82. Kevin

    Apr 22, 2007

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

    Reply

  83. Michael Hampton

    Apr 22, 2007

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

    Reply
  84. Apr 23, 2007

    Reply

  85. 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!

    Reply
  86. May 25, 2007

    Reply
  87. May 28, 2007

    Reply

  88. 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.

    Reply

  89. Tim

    Jun 14, 2007

    A marvellously simple fix… thanks for sharing…

    Reply

  90. Andrey

    Jun 28, 2007

    I love this hack !!! Good job …

    Reply

  91. 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!

    Reply
  92. Jul 12, 2007

    Reply

  93. 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 ?

    Reply

  94. 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

    Reply

  95. Ruriko

    Dec 23, 2007

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

    Reply

  96. n00b

    Dec 28, 2007

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

    Reply

  97. n00b

    Dec 28, 2007

    Oops. It removed the html tags.

    Reply

  98. n00b

    Dec 28, 2007

    Reply

  99. n00b

    Dec 28, 2007

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

    Reply

  100. 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!

    Reply

  101. 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.

    Reply

  102. Illy

    Jan 01, 2008

    Nice one just what i needed thanks a lot!

    Reply
  103. Feb 14, 2008

    Reply

Leave a Reply


Copyright © 2010 Homeland Stupidity.