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.
 
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?
 
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,
 
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:
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.
 
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.
 
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.
 
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.
 
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!
 
Ok, so this is what I did in the header...

$fileDir = $_SERVER["DOCUMENT_ROOT"];
require_once('/path-from-server-root/httpdocs/forums/src/XF.php');
\XF::start('/path-from-server-root/httpdocs/forums/forums');
$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();
$groupid = implode( ', ', $user->secondary_group_ids );

}else {
echo '';
}


So to check against the secondary group do something like this ...


<?php if ($groupid != '5') { ?>

Show this if they are not in secondary group 5

<?php } else {} ?>

Hope that helps @szymme
 
Hi, I am trying to achieve something similar, but getting a blank page. my ultimate goal is to have an array with userid, name, email, avatar

Code:
$cms = array(

        'id'=> $wpuser->ID,

        'user'=> $wpuser->display_name,

        'email'=> $wpuser->user_email,

        'avatar'=> get_avatar_url($wpuser->ID)

);

I have my forum in ./board and tried the script in both ./board and ../ still not working.
Code:
<?php
$fileDir = $_SERVER["DOCUMENT_ROOT"];
require_once('/home/sites/domaina.com/board/src/XF.php');
\XF::start('/home/sites/domaina.com/board');
$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();
$groupid = implode( ', ', $user->secondary_group_ids );

}
else 
{
echo 'Not logged in';
}


if ($groupid != '5') {

} else {

echo "no idea which group";
echo $user;
echo $groupid;

}

?>
 
Try a dump after the conditional.

PHP:
if ($groupid != '5') {
     dump($groupid);
}

Alternatively, you can place any dump for a different variable above that area to see if something is being returned.
 
Back
Top Bottom