-
Notifications
You must be signed in to change notification settings - Fork 18
Code Snippets
Alexander Schneider edited this page Nov 20, 2017
·
11 revisions
//Given that you already have the user group.
$userGroup->getFullUsers();
global $userAccessManager;
if (isset($userAccessManager)) {
$userGroupHandler = $userAccessManager->getUserGroupHandler();
$userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
\UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
$userId
);
}
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!';
}