Learn in this tutorial how to find out how many simultaneous accesses a WordPress user is having, using code.
In a couple of previous tutorials I have explained how to customize the number of simultaneous logins with the Loggedin plugin and a little bit of code.
This is very interesting if you want to offer different number of simultaneous logins in a membership, for example for groups.
With today’s tutorial you will see how you can check that the limits you have set, with the tutorials mentioned above or any other method, are being met.
Shortcode to display the number of simultaneous accesses for a user
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this case, you create a shortcode attribute to get the user ID and then use the WP_Session_Tokens
class to get the number of open sessions of the user. Once done, you display a message with the information you are interested in.
2. Insert the shortcode where you want
Finally, just type[user_sessions_count id="user-id"]
where you want it to be displayed and modify the id
of the user for whom you want to see the number of active sessions.
If you use, for example, [user_sessions_count id="77"]
it will display a message like, “User with ID 77 has 5 active sessions.”
Conclusion
As you can see, with a few lines of code you can monitor the number of open sessions of the user you are interested in.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉