Skip to content

Instantly share code, notes, and snippets.

@katienelson
Last active December 2, 2015 16:22
Show Gist options
  • Save katienelson/78ba62c3fcd9115039a2 to your computer and use it in GitHub Desktop.
Save katienelson/78ba62c3fcd9115039a2 to your computer and use it in GitHub Desktop.
Wordpress funtions to set and get a users last login activity (put in functions.php)
//associating a function to login hook
add_action('wp_login', 'set_last_login');
//function for setting the last login
function set_last_login($login) {
$user = get_userdata($login);
//add or update the last login value for logged in user
update_user_meta( $user->ID, 'last_login', current_time('mysql') );
}
//function for getting the last login
function get_last_login($user_id) {
$last_login = get_user_meta($user_id, 'last_login', true);
//picking up wordpress date time format
$date_format = get_option('date_format') . ' ' . get_option('time_format');
//converting the login time to wordpress format
$the_last_login = mysql2date($date_format, $last_login, false);
//finally return the value
return $the_last_login;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment