Limit only one session per user at a time in WordPress

There are many instances where you need to limit or restrict user from sharing his access to multiple people or places. For that we can add following code which will make sure “subscriber” role users will have only one session at a time.

How this works

When a subscriber user logs in to his account from a device, the following code will make sure he is logged out in all other devices / places where he was logged in before. Thus we make sure user got only one active session at a time.

Note: You can edit the above code to restrict other roles like “contributor” or “editor”. In above code, just change the role from “subscriber” to “editor” or whatever the role you want to apply this restriction.

Restrict sessions based on user info

You can restrict sessions specifically to a particular user based on their user ID or email or username. Sometimes you can use a custom usermeta too. Following sample explains that.

Share this post