Add the custom element based on the Joomla User Group

NOTE: This article explains adding custom code and using Joomla template overrides. It is imperative to have at least basic understanding of this Joomla functionality and know how to read and understand the code flow.


On our community website we are placing the mini badge in every activity stream item which help us distinguish between users with active, expired, or no license at all.


In our case, the little green badge in lower right-hand corner JS Active help us determine that user is having an active license, so our support team can react accordingly.
In other use cases, you might want to have a VIP users, or simply "tag" the people that belong to a different Joomla User Group like this (notice the Staff.png badge under avatar)

Staff badge.png

Template Override Is The Way To Go

While Joomla users are accustomed to use plugins to change the certain behavior of Joomla functionality, in this particular case it would be very hard to do it with a plugin because every Joomla site have different user groups, with the different unique ID which in turn would cause plugin to display nothing and give impression that it "don't work".
That said, we have to create template override for a place where we want our badge to show up.
We suggest you to read the Joomla documentation article about template overrides to get better understanding of this feature provided with every Joomla installation.

How To Find Joomla User Group ID

It is very simple. Just navigate to the Joomla backend and from the Users dropdown menu click on the Groups
The page with all the groups available will load, and you will be able to see the ID for every single one of them.

The Code You Need

Here is the block of code you need to use in your override

// Lets obtain the user object first
$juser = CFactory::getUser(); // Get the user object you want to display the badge for. On profile page you can use $juser = CFactory::getRequestUser(); to obtain the user object of the user we are looking at.
$juserid = $juser->id; // extract the user ID
// Now, lets get the array of all Joomla User Groups user is member of 
jimport( 'joomla.user.helper' );
$groups = JUserHelper::getUserGroups($juserid);
<!-- Display the badge if user is member of the Joomla User Group with ID 8 -->
<?php if(in_array(8, $groups)): ?>
	<img src="/path/to/badge.jpg"/>
<?php endif ?>

See Also