Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a site where users log in to view content specific to them. I would like to display the last date and time they logged in. How would I get this information from wordpress? If it doesn't exist, how could I add it?

Example: Welcome Back! Your last visit was on 10/25/2010 at 3:14pm.

Thanks!

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

I don't see anything like it in the database, so you probably have to do this yourself. To save the last login time, you can hook into the wp_login action, and save a user meta value (like [myprefix]_lastlogintime). You first read this value, so you get the previous login time, save this in the session, and then save the new login time.

On the regular admin pages you check whether this session variable is set. If it is, you display the welcome text and clear the session variable so you don't display it on every page.

If you want to save the last page visit time you have to write to the database on every (admin) page view. This is possible, but I would not recommend it. You can also save something once on logout (action wp_logout), but probably not everyone will remember to log out.

share|improve this answer
    
Can you please confirm to us that this approach is still working, nothing deprecated. Thank you very much. –  whiteletters and blankspaces Jun 8 '13 at 14:27
1  
@whitelettersandblankspaces: The hook still exists and is not marked as deprecated, so I have no reason to assume this would not work anymore. –  Jan Fabry Jun 8 '13 at 18:07
    
Thank you very much you gave me expert indice for it. The thing that was messing up my code is not wp_login but the way i was getting user id : wordpress.stackexchange.com/questions/101637/… thank you very much –  whiteletters and blankspaces Jun 8 '13 at 18:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.