Web Analytics

How to: Easily Track User Logins in WordPress

by Jason Unger, Founder

Photo by Miguel Á. Padriñán via Pexels

If you run a membership site using WordPress, there are so many little tweaks you can make to improve your community’s experience, while getting smarter about the data you collect.

On multiple occasions, we’ve been asked to help organizations better track when users log into their membership site. They usually want to know this because:

  • over the years, members have signed up but stopped using the site – meaning they can be purged from the user base
  • they want to target specific user groups (new users, frequent users, etc.) with specific content or offers

In WordPress, it’s easy to track this data – and all it takes is a quick code snippet.

Tracking Total Logins and Last Login Time

With one snippet, we can do two things: track the number of times a specific user has logged into your WordPress site, and track the most recent time they logged in.

First, let’s take a look at the code.

add_action('wp_login','user_last_login', 0, 2);
function user_last_login($login, $user) {  
    $user = get_user_by('login',$login);
    $now = time();
    $logintimes = get_user_meta( $user->ID, 'times_logged_in', true);
    if ($logintimes == '') { 
        $logintimes = 1; 
    } else {
        $logintimes++;
    }
    update_user_meta( $user->ID, 'user_last_login', $now );
    update_user_meta( $user->ID, 'times_logged_in', $logintimes );
}

Here’s how it works.

First, we’re hooking into the wp_login hook, which happens when a user logs into your site. Our function, user_last_login, accesses the user $login variable using the get_user_by() function.

Second, we’re getting the current time using the time() PHP function.

Now comes the fun part.

We’re checking to see if this user has an existing custom field called “times_logged_in” attached to their user profile. If times_logged_in doesn’t exist, then we’re going to create it, and assign a value of 1. Congratulations, they’ve now signed into your site!

If times_logged_in does exist, then we’re going to grab that value and add 1 to it – iterating the value so we’re counting up the number of times they’ve logged in.

Lastly, we’re overwriting another custom field – “user_last_login” – with the current time that we just grabbed. For this value, it doesn’t matter if the user has logged in previously or not; we’re always going to overwrite this field with the latest value so it reflects our goal: tracking the most recent time the user logged in.

Bonus Code: Custom Redirect to Onboarding

Creating an Onboarding Experience for your membership site’s users can be incredibly valuable. It can help show them how to use your site, what to do if they need help, and how to get the most value out of their membership.

Using our login tracking snippet, we can simplify their experience when they first login to your site – redirecting them to your onboarding.

Here’s what that code looks like.

function login_redirect( $redirect_to, $request, $user ){
	$timeslogged = get_user_meta( $user->ID, 'times_logged_in', true);
    if ($timeslogged == 1){
	return home_url() . '/onboarding/';
    } else {
    	return home_url();
    }
}
add_filter( 'login_redirect', 'login_redirect', 10, 3 );

It’s pretty simple: we grab that ‘times_logged_in’ value from their user profile, and if it equals 1 (because it’s their first login in), we redirect them to the onboarding URL.

If they’ve logged in more than once, we redirect them to the homepage.

Have questions about building a membership site or getting better data on your users? Reach out to us and let’s chat.

Avatar photo
About Jason Unger

Jason Unger is the Founder of Digital Ink. He built his first website on Geocities, and hasn't looked back since. Digital Ink tells stories for forward-thinking businesses, mission-driven organizations, and marketing and technology agencies in need of a creative and digital partner.

Other Stories You May Like

What’s your story?

Let’s share it with the world.

Let’s Do This

Close Window
All the Cool Kids are Doing it

Sign Up for the Digital Ink Newsletter

All the cool kids are doing it.