XF 2.2 External call to get username/usergroups

Rushster

Member
I have a wordpress site and I am trying to get the username info and usergroup info to be called into a WP template. The purpose of this is to check if they are in X usergroup to hide certain bits of content.

I know that using the following in a template spits out the logged-in username which works, but it wipes all the WP content and just displays the username on the page.

<?php
$fileDir =$_SERVER["DOCUMENT_ROOT"];
require_once($fileDir . '/forums/src/XF.php');
\XF::start('/hc');
$app = \XF::setupApp('XF\Pub\App');
$s = $app->session();
$uid = $s->get('userId');
if ($uid){
$finder = \XF::finder('XF:User');
$user = $finder->where('user_id', $uid)->fetchOne();
print_r($user->username);
}else{
echo 'guest';
}

?>

I haven't gone any further with this until I can find out what I need to do to keep the WP content intact when this is called and also how to get the usergroups the logged in persons is in.

My PHP is not brilliant so any help here would be appreciated.
 

Kruzya

Well-known member
This may work.
PHP:
<?php

/**
 * This code should be inserted in start of file,
 * where you need show username and group.
 */

$forumRoot = '.../forums/';
require_once($forumRoot . '/src/XF.php');

\XF::start($forumRoot);
$app = \XF::setupApp('XF\App');
$app->start(false);

$session = $this->session();
if (!$session->exists())
{
    $this->onSessionCreation($session);
}

$user = $this->getVisitorFromSession($session);
?>
PHP:
<?= /** This code should be inserted in place, where you need show username. */ $user->username ?>

With usergroup i have question.
You need show all user groups or only with highest display styling priority?
 

Brogan

XenForo moderator
Staff member
Could you use the API for this?

Using a super user key and passing 'XF-Api-User' in the header will return the primary and secondary group IDs (along with the other data).
Code:
"secondary_group_ids": [
    3,
    4,
    5,
    12,
    15,
    18,
    20,
    21
],
"show_dob_date": true,
"show_dob_year": false,
"signature": "",
"timezone": "America/New_York",
"trophy_points": 2553,
"usa_tfa": false,
"user_group_id": 2,
 

Rushster

Member
I just need to check if the logged-in person is in a specific usergroup then I assume I can just use "if" statements to show or hide the content?

I can get it to display the username now, it's the usergroup that's the important bit but I am not sure about how to do that.
 
Last edited:

Rushster

Member
Could you use the API for this?

Using a super user key and passing 'XF-Api-User' in the header will return the primary and secondary group IDs (along with the other data).
Code:
"secondary_group_ids": [
    3,
    4,
    5,
    12,
    15,
    18,
    20,
    21
],
"show_dob_date": true,
"show_dob_year": false,
"signature": "",
"timezone": "America/New_York",
"trophy_points": 2553,
"usa_tfa": false,
"user_group_id": 2,
The question I have is, how do I get that array and set conditionals against the result. I am assuming I can get the user_group_id with the finder?

I am just not sure what syntax to use in the PHP code to do this.
 

Brogan

XenForo moderator
Staff member
I'm not sure what code you're using in WP and whether you're doing it in PHP or templates, but you can just check the secondary group IDs array and use an if against a specific value.
 

Rushster

Member
I'm not sure what code you're using in WP and whether you're doing it in PHP or templates, but you can just check the secondary group IDs array and use an if against a specific value.
It's in a WP header template. What I don't know how to do is get the secondary group ID's out as an array that's readable.
 

Brogan

XenForo moderator
Staff member
I'm not familiar with WP template code but presumably there is some way of plucking a value from an array.

Try posting in the WP support forums for help with their software.
 

Rushster

Member
I'm not sure what code you're using in WP and whether you're doing it in PHP or templates, but you can just check the secondary group IDs array and use an if against a specific value.
Actually, your comment pushed me in the right direction and I finally got it working. My brain wasn't engaged in WP mode and was focusing on Xen :) So thanks for that!
 
Top