WordPress.org

Ready to get started?Download WordPress

Forums

Capture Users Last Login datetime (7 posts)

  1. j8h9
    Member
    Posted 4 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
    Member
    Posted 4 years ago #

    Catch it with the 'wp_login' hook.

    add_action('wp_login','your_function_name');

    '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
    Member
    Posted 4 years ago #

    I use two plugins for this.

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

    login-logger

    The second one lists every login by every user.

    log-user-access

  4. shetakaey
    Member
    Posted 4 years ago #

    Awesome, lokrin2000! Thank you for the links.

  5. flatsphere
    Member
    Posted 4 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
    Member
    Posted 4 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
    Member
    Posted 3 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'));
    }
    add_action('wp_login','appthemes_last_login');
    
    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:

    <?php
    global $userdata;
    get_currentuserinfo();
    ?>
    
    <?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.

    ~David
    AppThemes

Topic Closed

This topic has been closed to new replies.

About this Topic