
Displaying Last Login Time of Users in WordPress
- October 21, 2016
- Leave a comment
Member oriented and user profile managing sites may need the functionality of showing last login of users on their WordPress Dashboard. Using wp_login hook, last login display functionality can be achieved easily.
To record the user activity of last login, you need to add the following code in your functions.php file:
1 2 3 4 5 6 7 8 |
<?php function user_last_login_record( $user_login, $user ) { update_user_meta( $user->ID, 'last_login', time() ); } add_action( 'wp_login', 'user_last_login_record', 10, 2 ); ?> |
The following code will return the last login time. You can use it directly in your theme template by calling the function. Additionally, it will register a shortcode for the same function so that it can be inserted later in WordPress back-end pages/posts.
1 2 3 4 5 6 7 8 9 10 |
<?php function wpb_last_login_time() { $last_login = get_the_author_meta('last_login'); $the_login_date = human_time_diff($last_login); return $the_login_date; } add_shortcode('last-login-time','wpb_lastlogin'); ?> |
To display last login time, insert the registered shortcode i.e. [last-login-time] in back-end pages/post. Use the following command to insert the shortcode in template files:
1 |
<?php echo 'Last seen: '. do_shortcode('[last-login-time]') .' ago'; ?> |
User Comments