Sometimes you may find that you want to separate the content your logged-in users see from what your unregistered users see; things like a log-in form, or a call to action to register should only be seen by users that aren’t logged-in already, and buttons might change function or their look depending on whether the user is logged-in or not. On the GavickPro site, for example, the “Log In” button in the top right changes to a “My Account” link once login is complete. There’s a lot of other examples, and plenty of ways to diversify your content with this method.
We often see questions in our forum from users interested in applying content separation like above in their own site, but they’re not quite sure how to do it, so we’ve put together this handy little guide to managing the displayed content depending on a user’s currently login status in Joomla. This guide does rely on a bit of coding know how, so you’ll need to be familiar with the basics of PHP code to take advantage of it.
Joomla User Object
The first thing we’ll need to do is define a variable:
$user = JFactory::getUser();which returns a reference to the global user object, only creating it if it finds that the object has not already been created. The object returned will be a JUser type object. The object JUser getUser( $id ) loads the user; if the argument ($id) is omitted or is null then the current user will be loaded.
Now we’ve got a variable which will take a user, either the current user or one defined by an id, we can create a condition that will check if the Joomla user in question is logged-in or not. By checking whether the user Id is greater than 0 (when a user is not logged-in the user id is always 0) we can tell if a user is logged-in. Let’s put this logic in code form:
$user = JFactory::getUser(); if($user->id > 0) { // code to display for the registered users }Which will execute the code in the if statement only for registered users.
If we want to separate the content for unregistered users (guests) only, we can check if the user id is equal to 0 instead:
$user = JFactory::getUser(); if($user->id == 0) { // code to display for the guest users }
Examples
Display welcome message for logged in users
Let’s say you want to display a personalized welcome message in your template; the required code should look something like this:
<?php $user = JFactory::getUser(); if($user->id > 0){ echo "Welcome " . $user->username; } ?>where $user->username will display the username of the current user.
Display module position only for non registered users
Let’s get a bit more complex, and set a particular module position’s contents, for example, bottom5, only to users who aren’t currently logged-in. As a base we’ll use our Simplicity template; the changes will be similar in other templates, but of course the module positions may be very different. To make this change, we have to edit the gk_simplicity/layouts/default.php file and change this fragment:
<?php if($this->API->modules('bottom5')) : ?> <section id="gkBottom5"> <div class="gkCols6<?php if($this->API->modules('bottom5') > 1) : ?> gkNoMargin<?php endif; ?> gkPage"> <jdoc:include type="modules" name="bottom5" style="<?php echo $this->module_styles['bottom5']; ?>" modnum="<?php echo $this->API->modules('bottom5'); ?>" /> <!--[if IE 8]> <div class="ie8clear"></div> <![endif]--> </div> </section> <?php endif; ?>to:
<?php $user = JFactory::getUser(); ?> <?php if($user->id == 0) : ?> <?php if($this->API->modules('bottom5')) : ?> <section id="gkBottom5"> <div class="gkCols6<?php if($this->API->modules('bottom5') > 1) : ?> gkNoMargin<?php endif; ?> gkPage"> <jdoc:include type="modules" name="bottom5" style="<?php echo $this->module_styles['bottom5']; ?>" modnum="<?php echo $this->API->modules('bottom5'); ?>" /> <!--[if IE 8]> <div class="ie8clear"></div> <![endif]--> </div> </section> <?php endif; ?> <?php endif; ?>
Now,modules from this position will be visible only to unregistered Joomla users (guests).
Display different text (label of the button or links) depending on registration state
In this example we’ll use the Simplicity Joomla template once again. We’ve added a login button that links to the login form, and we’ve also added a gkLogin Additional class for this menu item. Now when this button is clicked, and the user logs-in to Joomla, this button will still be visible as “Login” which doesn’t look good and can be confusing for your users. It’s time to change it.
We have to edit the lib/menu/GKBase.class.php file and in the genMenuItem method add these fragments:
if(stripos($active, 'gkLogin') !== FALSE) { $user = JFactory::getUser(); if($user->id > 0) { $txt = JText::_('TPL_GK_LANG_MYACCOUNT'); } }after this line:
$txt .= $tmpname;
This fragment will replace the text in the menu item that has the “gkLogin” CSS class assigned to it only when the user is logged in. Of course you’ll also have to add a bit of text to the language file, like this:
TPL_GK_LANG_MYACCOUNT="My account"
Now when you are logged-in your menu item should look like: