How to set User Last Login Date and Time in WordPress Without Plugin |
X

Congrats, You are Subscribed to Receive Updates.

How to set User Last Login Date and Time in WordPress Without Plugin


How to set User Last Login Date and Time in WordPress. Usually we don’t mind install a plugin for a small operations. But a small piece of code can help you to get it done it. I hope this one solves you not to use plugin for users last login access. Let’s move to code.  Here we are going to use two user meta’s to perform it. `last_login, current_login`. With help of these two parameters we used to store the users login access time.  Let me show you the function.

We will add an action hook to run it on run over function on every login.

add_action('wp_login', 'set_last_login');

And we will pass the $user_login to it. With that we can get current users login id and his details, than we can use get user meta function getusers existing current login time and with that we will validate and add the functions.

//function for setting the last login
function set_last_login($login) {
	$user = get_userdatabylogin($login);
	$curent_login_time = get_user_meta(	$user->ID , 'current_login', true);
	//add or update the last login value for logged in user
	if(!empty($curent_login_time)){
		update_usermeta( $user->ID, 'last_login', $curent_login_time );
		update_usermeta( $user->ID, 'current_login', current_time('mysql') );
	}else {
		update_usermeta( $user->ID, 'current_login', current_time('mysql') );
		update_usermeta( $user->ID, 'last_login', current_time('mysql') );
	}
}

and getting the details through a function and here is the code for it.

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');

   
	if(wp_is_mobile()) {
		$the_last_login = date("M j, y, g:i a", strtotime($last_login));  
	}else {
		$the_last_login = mysql2date($date_format, $last_login, false);
	}
   return $the_last_login;
}

Where you want to show. just call this function with user ID. You can see the date and time there.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

4 comments

  1. commenter

    That’s very handy, thank you ! 🙂

    Also I have question, is it possible to find user login dates from wordpress without any extra code?

    To clarify, I don’t have any user meta catching functions/plugins. I would like to check last few logins of admin user.

    Does WP stores meta info about logins somewhere in database?

    I would be very happy if you could give me some hint, maybe it’s simply impossible to check it and I have to look on server logs etc
    Cheers 😉

    • commenter

      There is no clue with default wordpress to get users login history. There are some plugins available. I don’t have any information about third party plugins. But definitely, WP wont store it. It stores the browser and OS of every users, when you login. But it won’t save a history of login access time for a user.

  2. commenter

    Hello,

    Thank you for your great work

    Im new to wordpress, php and mysql, but i managed to make a script that get the value of “wc_last_active” (i believe its from woocommerce) but the data there is not correct, not sure why.

    I’ve came across this post, and i have some questions…

    Where do you exacly put the code? you just create a new php file, or do i have to add that code to a specific file?
    And if you have to put it in a specific file (i imagine its login.php), will the code disappear if/when i update wordpress?

Reply to Varadharaj V Cancel reply

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar