Skip to content
Alexander Schneider edited this page Nov 20, 2017 · 11 revisions

Get all user of a user group

//Given that you already have the user group.
$userGroup->getFullUsers();

Get all user groups for user

global $userAccessManager;

if (isset($userAccessManager)) {
    $userGroupHandler = $userAccessManager->getUserGroupHandler();
    $userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
        \UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
        $userId
    );
}

How to tell if the logged in user is in a group?

Taken from https://wordpress.org/support/topic/how-to-tell-if-the-logged-in-user-is-in-a-group/ - Thanks to Christian Leuenberg

A function that you can put into your themes function.php:

function uamIsUserInGroup($groupName='', $userId='') {
    if (empty($userId)) {
        $userId = get_current_user_id();
    }

    global $userAccessManager;

    if (isset($userAccessManager)) {
        $userGroupHandler = $userAccessManager->getUserGroupHandler();
        $userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
            \UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
            $userId
        );

        foreach($userGroupsForUser as $element) {
            if ($element->getName() == $groupName) {
                return true;
            }
        }
    } else {
        return false;
    }
}

And then just call it like this with group name and an optional user id as second parameter:

if (uamIsUserInGroup('MyGroupName')) {
    echo 'User is in group!';
} else {
    echo 'User is not in group!';
}