scriptygoddess RSS Feed
 
 
 
 

Add a "remember me" checkbox to the login form

Neil had asked about adding a "remember me" checkbox to the login form. The idea here is that if you're really forgetful (like me! Ack!) and you know you're really forgetful – and you know you're probably going to forget to "logout" before closing your browser – if you don't click the "remember me" checkbox – then when you close your browser, you will be automatically logged out of WordPress. (This is good when you're working on a shared computer)

It does require some core WP editing – minor tweaks to just the wp-login.php page – but wanted to warn you up front (I have to apologize for the core edits. I'm more than welcome to hear suggestions of how one can do this WITHOUT doing the core editing – but I just can't come up with it)

Open up wp-login.php. Look for the following codes (should be on lines 389 and 391) :

setcookie('wordpressuser_'.$cookiehash, $user_login, time() + 31536000, COOKIEPATH);
setcookie('wordpresspass_'.$cookiehash, md5($user_pass), time() + 31536000, COOKIEPATH);

and replace those two lines with the following:

if (isset($_POST['rememberme'])) {
setcookie('wordpressuser_'.$cookiehash, $user_login, time() + 31536000, COOKIEPATH);
setcookie('wordpresspass_'.$cookiehash, md5($user_pass), time() + 31536000, COOKIEPATH);
} else {
setcookie('wordpressuser_'.$cookiehash, $user_login, NULL, COOKIEPATH);
setcookie('wordpresspass_'.$cookiehash, md5($user_pass), NULL, COOKIEPATH);
}

(Passing "NULL" suggestion by Andrew in the comments thread) Then look for what will now probably be on line 313:

<p><label><?php _e('Password:') ?> <input type="password" name="pwd" value="" size="20" tabindex="2" /></label></p>

And add this just below it

<p><label>Remember me? <input type="checkbox" name="rememberme" value="rememberme" tabindex="3"></label></p>

Then, if you want to be "auto-logged-out" when you close your browser, just make sure you DON'T click that "remember me" checkbox

Post updated 10/7/2004 to reflect changes in WP 1.2.1

13 Responses to “Add a "remember me" checkbox to the login form”

  1. 1
    Neil Stead:

    Jennifer, you're just a marvel! Only asked for this yesterday, and here it is already! I look forward to trying it out – Thanks!

    (NB: My URI points to my club's current website – for a look at how I'm changing it to use WP, have a look at the new site – very much under construction!).

  2. 2
    Mark J:

    Jenn,
    I'm almost positive that there is currently no way to do this with a plugin.

    Even if you could harness a hook to set a cookie, I've only run into trouble when trying to set the same cookie multiple times in the same script execution.

    Of course, you COULD just offer the modified wp-login.php for download. Heck, that's easier than a plugin, because you don't have to go in and activate it!

  3. 3
    Carthik:

    I have seen this request a few times over at the forums. Maybe you can send a diff to matt so he can think of including it.

  4. 4
    david:

    Aha I finely figured it out :)

  5. 5
    Code Novice:
    Bookmarks: MT, WP and Moneymaking Blogs
    Scriptygoddess continues to post numerous WP-related fixer-uppers with three new offerings:
    Adding content to the spam list
    Delete all comments via moderation
    Add a 'remember me' option on login page
    David Raynes posted his latest MT …

  6. 6
    Andrew:

    I'm glad my comment about passing NULL could be useful.

  7. 7
    Mama Write:
    Various WordPress Links
    Rather than ping ScriptyGoddess multiple times and clog my sideblog with even more WordPress links, I decided to plop 'em all here. I'm going to give WordPress a try again on my course website, since there are various hacks that…

  8. 8
    chuguo:

    thanks!

  9. 9
    Neil Stead:

    Just discovered that if a user changes their password, WP sets the cookie for the new password – so that it will always be remembered.

    I think the easiest solution is to change the code in profile.php so that it just sets a session cookie – the user will have to log back in next time he comes to the site.

    Replace:
    setcookie('wordpresspass_'.$cookiehash, md5(md5($newuser_pass)), time() + 31536000, COOKIEPATH);

    with

    setcookie('wordpresspass_'.$cookiehash, md5(md5($newuser_pass)), NULL, COOKIEPATH);

    There may be a more elegant way of doing this – by finding out the current expiry date of the cookie, for example, but I don't know how.

    Neil.

  10. 10
    Tin:

    hi. for some reason this doesnt work for me. everything runs smoothly but when i close my browser and reopen it, i'm still logged in. i followed all your directions closely. do you have a wp-login.php already prepared so i can download? i'm wondering if i did something wrong with the modifications.

  11. 11
    Stacey:

    Is there a way to make a "remember me" option for comments?

    For some reason it isn't working on my site.

  12. 12
    james:

    I am in the same boat as Tin.

    I modified the code and nothing changed. Everything still works fine, but I am still permanently logged in even after I close my browser.

    I am using worpress 1.2.1

    Any ideas? It would be nice to see a full wp-login.php file from somebody who has managed to make it work.

    This would be great, if I could get it working.

    Thanks!

  13. 13
    Remember Me for logins : [ mkhairul.com ]:

    [...] Scriptygoddess – Add a remember me checkbox to the login form [...]

Categories

Archives

Bookmarks

WordPress Resources

Meta

ADVERTISEMENTS