• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Return secondary usergroup a current user is a member

LPH

Well-known member
#1
The following returns the usergroup ID but now I'd like to match the current user logged in with their secondary usergroup.

Code:
$user_groups = explode(',', $secondary_group_ids );
$user_groups[]= $user_group_id;

Basically ... along this line of thinking....

If the currently logged in user is a member of usergroup 2 then do something.

Is this possible?
 

Chris D

XenForo developer
Staff member
#2
PHP:
$userModel = XenForo_Model::create('XenForo_Model_User');
if ($userModel->isMemberOfUserGroup($user, 2, true))
{
    // Do something
}
$user is your user which must include the user_group_id and secondary_group_ids.

2 is the user group you want to check. This can actually also be an array of usergroup IDs.

It's worth noting that the "true" in the callback is un-needed here, it is true by default. i.e. without it it will check the user against secondary usergroups. If you set it to false, however, it would only check primary groups.
 

LPH

Well-known member
#4
I'm sorry. I'm staring at the code and thinking ... how do I get the secondary usergroup for the current user? :)

In copying the code, the following error was returned:

Code:
Catchable fatal error: Argument 1 passed to XenForo_Model_User::isMemberOfUserGroup() must be of the type array, null given, called in /path/to/wp-content/plugins/xenword-user-info.php on line 43 and defined in/path/to/community/library/XenForo/Model/User.php on line 1659

Am I changing the 2 to a "6" or "5" or "7"?
Do I define a variable?

Update: And I tried:

Code:
echo '<br />The XF secondary usergroup' . strpos( $user['secondary_group_ids'] ) . '<br />';
But that returned NULL.
 
Last edited:
#5
Dump the $user variable it seems that it's empty. it can be either there is no logged in user or you didn't pull the user info. Looking at the the error code and line if you are trying to incorporate this into a third party script try using the XenForo SDK.
 

LPH

Well-known member
#6
Dump the $user variable it seems that it's empty. it can be either there is no logged in user or you didn't pull the user info. Looking at the the error code and line if you are trying to incorporate this into a third party script try using the XenForo SDK.
The plan was to start using the SDK after this latest version of the script was finished but I'm tracking down a really nasty issue with matching the secondary usergroup and WordPress roles.
 

LPH

Well-known member
#7
Code:
        $user_groups = explode( ',', $secondary_group_ids );
        $user_groups[] = $user_group_id;
        $user_roles = array();
        foreach( $user_groups as $user_group ) {
            $user_roles[] = $XF->options['xf_user_role'][$user_group];
            wp_die( 'This is the role:' . var_dump( $XF->options['xf_user_role'] ) );
        }
returns

Code:
array(7) { [3]=> string(13) "administrator" [4]=> string(6) "author" [2]=> string(10) "subscriber" [1]=> string(10) "subscriber" [5]=> string(6) "author" [7]=> string(11) "contributor" [6]=> string(6) "editor" }
Now I just need to figure out how to get the current user's user_group, return the paired role in quotes, then set_role ('').

I can pencil it out, write about it here, but the code escapes me. I've tried different ways to get the current user group. I realize @Chris Deeming gave a huge push in the right direction with public function isMemberOfUserGroup(array $user, $userGroupId, $includeSecondaryGroups = true) in User.php ...

Update: Almost there !
Code:
$XF->options['xf_user_role'][$user_group]
Returns string(13) "administrator" for the user with that XF secondary group. ... so I need to strip out the string(13) as well as the quotes ...
 
Last edited: