Homeland Stupidity

WordPress sidebar login form hack

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: 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">
  global $user_ID, $user_identity;
  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>
  <h2><?php echo $user_identity; ?></h2>
    <?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>