Capture Users Last Login datetime (7 posts)

  1. j8h9
    Posted 6 years ago #

    Can someone point me to a simple hack for capturing users last login? I added a column to the user table to hold the value, I having difficulty understanding where to hack the code to update this col when user logs in... Is it me or is much of the core code not indented/aligned per common standards? Thanks to all who respond! WordPress is the best.

  2. s_ha_dum
    Posted 6 years ago #

    Catch it with the 'wp_login' hook.


    'your_function_name' is the name of the function that will actually handle insert. It should look something like this:

    function my_last_login($login) {
        global $user_ID;
        $user = get_userdatabylogin($login);
        update_usermeta( $user->ID, 'my_last_login', time() );

    My function uses the wp_usermeta table to hold the data, rather than using separate column.

  3. lokrin2000
    Posted 6 years ago #

    I use two plugins for this.

    The first one lists each user once with their last login.


    The second one lists every login by every user.


  4. shetakaey
    Posted 6 years ago #

    Awesome, lokrin2000! Thank you for the links.

  5. flatsphere
    Posted 6 years ago #

    Hi apljdi, your solution looks like the perfect one for me, can you please let me know which files need these snippets?

    Many thanks

  6. Tosh
    Posted 6 years ago #

    @apljdi That's great. I want to do something like this:

    If the time since last login was say 1 month run this action. How would I check the time?

  7. David C
    Posted 5 years ago #

    apljdi, that's a great idea. Thanks for sharing. I ended up creating two functions (based off your original one) and pasted them in my functions.php file.

    function appthemes_last_login($login) {
        global $user_ID;
        $user = get_userdatabylogin($login);
        update_usermeta($user->ID, 'last_login', current_time('mysql'));
    function appthemes_get_last_login($user_id) {
        $last_login = get_user_meta($user_id, 'last_login', true);
        $date_format = get_option('date_format') . ' ' . get_option('time_format');
        $the_last_login = mysql2date($date_format, $last_login, false);
        echo $the_last_login;

    Then to call this from any of your theme pages, just paste in this code:

    global $userdata;
    <?php _e('You last logged in at:','appthemes'); ?> <?php appthemes_get_last_login($userdata->ID); ?>

    This makes it easy to pull the last login date and also it also uses the WordPress date/time formats you set on your blog settings page. Hope that helps you all.


Topic Closed

This topic has been closed to new replies.

About this Topic