NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Easy
Web Hosting Canada

Last modified : Sep 20 2022

Estimated reading time : 1 minute, 58 seconds - 125 words

To create the user login count, we need three new functions to add inside your wp-login.php file. With this tutorial, you will be able to see the number of times your WordPress users logged in by adding a new column in the users’ table.

We will create a new meta user in order to display the number of log in by buers.

WordPress User Login Count

The meta user will be titled ‘user_count_connections’ and automatically add every time a user logs in.

function via_login($username, $user) {
    $login_count = intval(get_user_meta($user->ID, 'user_count_connections', true));
    $login_count++;
    update_user_meta($user->ID, 'user_count_connections', $login_count);
}
add_action('wp_login', 'via_login', 10, 2);

Now we’ll create the new column under the Users tab to display the number of connections.

function via_modify_user_table( $column ) {
$column['connections'] = 'Connections';
return $column;
}
 
add_filter( 'manage_users_columns', 'via_modify_user_table' );

And finally we’ll display the meta user results.

function via_modify_user_table_row( $value, $column_name, $user_id ) {
	$user = get_userdata( $user_id );
    switch ($column_name) {
        case 'connections' :
             $user_connection = get_the_author_meta( 'user_count_connections', $user_id );
	     if ($user_connection == 0) {
	        return '0';
	     }
	     else {
	        return get_the_author_meta( 'user_count_connections', $user_id );
	     }
            break;
        default:
    }
    return $value;
}
add_filter( 'manage_users_custom_column', 'via_modify_user_table_row', 10, 3 );

Moving forward, in WordPress under the User tab, you’ll see now many times each user logged in.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>