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 using the code below to track when a user last logs in. I'm realizing though that if they checked the Remember Me option, this won't track their last visit.

// Catch the time they login and save it
function set_last_login($login) {
  $user = get_userdatabylogin($login);
  update_usermeta( $user->ID, 'last_login', current_time('mysql') );
}
add_action('wp_login', 'set_last_login');

// Function to display the time
function 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);
  return $the_last_login;
}

In addition to the working code above for tracking login, I'm looking to see when the user was last active if they are already logged in.

Any tips?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You could create a new entry for the user meta data and update it on every admin pageload.

Put the following in your theme's functions.php or maybe wrap it into a plugin:

function update_last_action_time() {
    $user = wp_get_current_user();
    update_user_meta($user->ID, 'last_action_time', current_time('mysql'));
}
add_action('admin_init', 'update_last_action_time');

Of course, you could refine this (and thus lower the overhead) by restricting this to certain pages, for instance, the dashboard only.

// Edit: you should use update_user_meta instead of the deprecated update_usermeta.

// Edit, again

In case you want to track users on the front-end, put this code in your desired template:

if (is_user_logged_in()) {
    $user = wp_get_current_user();
    update_user_meta($user->ID, 'last_action_time', current_time('mysql'));
}

Again, to lower the overhead, I'd prefer putting this not into the footer.php file. I'd rather take home.php (if you have one), or check for particular requests (e.g., your home page, meaning: home_url() == 'http://'.$_SERVER['SERVER_NAME']). But as said before (meaning: below ;)), the overhead is, of course, okay when using it nonetheless in footer.php.

share|improve this answer
1  
This is a good start for me, thanks! I restrict admin access, but perhaps if they go to their member page, or even the front page I'd track it. I'll have to contemplate this one for a bit to make sure I'm not adding too much overhead. Thanks for the update_user_meta catch too. –  Pat Mar 25 '13 at 13:38
    
Do you think adding something like this to wp_footer would add a lot of overhead since it's just writing a quick field? –  Pat Mar 25 '13 at 18:09
    
Well, wp_footer is the wrong hook, since you only want to track logged in users. Keep it either hooked in admin_init (as stated above) or restrict it to certain admin pages. –  t f Mar 25 '13 at 18:13
    
Yeah you're right, I had a brainfart. I meant the footer of my theme. Not hooking it in, but running something like <?php update_last_action_time(); ?> when the footer of the theme is called. (Of course, adding a little more heavy lifting to the function.) I'm concerned about overhead, but thinking this would be minimal? –  Pat Mar 25 '13 at 18:16
    
Of course, the overhead is minimal, I just named it overhead, because most of the time you'll be updating the value while you actually don't need it. That's why I stated to restrict it to your needs. But again, when talking about the footer of your theme, it is the front-end you're talking about. If you want to track logged-in users on the back-end (meaning admin sites), then put the code into the functions.php (hooked into admin_init, as mentioned before). For tracking logged-in users reading your website, put it in the footer for instance, but check if (is_user_ logged_in()) .... –  t f Mar 25 '13 at 18:29
show 2 more comments

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.