Custom session limit for specific user

In some cases you might want to set a custom session limit for a user. This becomes a really handy snippet if for example an external api or some custom usermeta determines how many sessions a user is allowed to have.

// Set limit for user with ID 8 to 10 sessions
add_filter("session_limit", "increase_limit_user", 99, 2);
function increase_limit_user(int $limit, WP_User $user) {
    if ( $user->ID == 8 ) {
        return 10;
    }
    return $within;
}