Are you developing a website with user management features? And you have a need to know when the last time the user logged in.
If you need such a feature, then this article is for you. But first, here’s what you get after reading this article:

Store the last login of the user
Before displaying the last login date or time, we must collect it first. WordPress does not collect or store this data by default, so we have to do it ourselves.
There are two ways to login in WordPress:
- Based on the form
wp-login.php - Or rely on the
wp_signon()function
Luckily, the action hook wp_login works for both!
add_action( 'wp_login', 'hk_collect_login_timestamp', 20, 2 );
function hk_collect_login_timestamp( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span>
</div>
All you need to do is add the above code to the functions.php file of your theme or child theme. From this point on, the collection of user’s last login data is just starting to be enabled.
Data display
I will create a column to display the data on the “All Users” page. To do this, I will use the following code:
add_filter( 'manage_users_columns', 'hk_add_last_login_column' );
add_filter( 'manage_users_custom_column', 'hk_last_login_column', 10, 3 );
function hk_add_last_login_column( $columns ) {
$columns['last_login'] = 'Last Login'; // column ID / column Title
return $columns;
}
function hk_last_login_column( $output, $column_id, $user_id ){
if( $column_id == 'last_login' ) {
$last_login = get_user_meta( $user_id, 'last_login', true );
$date_format = 'j M, Y';
$output = $last_login ? date( $date_format, $last_login ) : '-';
}
return $output;
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span>
</div>
Once completed, the members table will appear a new column named “Last Login” .

Make the data column sortable
I think this is a pretty useful step because it allows you to quickly find out who has recently logged in. Use the code below to make the “Last Login” column sortable.
add_filter( 'manage_users_sortable_columns', 'hk_sortable_columns' );
add_action( 'pre_get_users', 'hk_sort_last_login_column' );
function hk_sortable_columns( $columns ) {
return wp_parse_args( array(
'last_login' => 'last_login'
), $columns );
}
function hk_sort_last_login_column( $query ) {
if( !is_admin() ) {
return $query;
}
$screen = get_current_screen();
if( isset( $screen->id ) && $screen->id !== 'users' ) {
return $query;
}
if( isset( $_GET[ 'orderby' ] ) && $_GET[ 'orderby' ] == 'last_login' ) {
$query->query_vars['meta_key'] = 'last_login';
$query->query_vars['orderby'] = 'meta_value';
}
return $query;
}
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span><span>32</span><span>33</span>
</div>
Epilogue
I hope this article will help you to manage users on the site more effectively. Especially for those of you who are developing websites related to community development.
If you found this article helpful, please comment and share this article. In addition, you can follow the WordPress Tips section and follow Facebook for more new knowledge.

