Home » Php » Get user group id in Joomla with PHP

Get user group id in Joomla with PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

To get the user group id in Joomla 2.5.15, I followed the documentation in Joomla Documentation here

My code looks like this:

$user = JFactory::getUser();
echo "<p>Your group id is {$user->gid}.</p>";

But this code is producing the error: Notice: Undefined property: JUser::$gid in
Without any group id in the output.

How to&Answers:

Try using this:

$user   = JFactory::getUser();
$groups = $user->get('groups');

foreach ($groups as $group)
{
    echo '<p>Group = ' . $group . '</p>';
}

Answer:

jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser($user_id, false);
var_dump($groups);

Answer:

FROM DOCS

$user = JFactory::getUser(99); 
echo "<p>Your usertype is {$user->usertype} which has a group id of {$user->gid}.</p>";