WordPress.org

Support

Support » Plugins and Hacks » Capture Users Last Login datetime

Capture Users Last Login datetime

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    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

    Awesome, lokrin2000! Thank you for the links.

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

    Many thanks

    @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?

    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Capture Users Last Login datetime’ is closed to new replies.
Skip to toolbar