Show Different Content to Different Joomla Users
Thursday, 22 July 2010 09:47

Yesterday I showed how to Hide Columns When Editing the Front End of Joomla as it often causes overlap issues that make editing difficult. In this post I'll show a quick and easy way to show different content to different users.

Being able to show different content to registered and logged in visitors is a very common request for a Joomla site. By default, you can have Joomla show the "intro" copy to guests and they must to log in to see the rest. But what if you want to show some specific content to guest visitors, and then different content to registered users once they have logged in.

There are a couple of extensions that give this functionality, but as a rule of thumb, I'll always trying and use the core if I can to get what I want. In this case, I figured out a simple method that worked for my needs.

First we need to make available information (aka accessing the user object) about the user, such as their name, or in this case, their user level.

To do this, we insert the following in the head of our template:

$user =& JFactory::getUser();

We can now echo all sorts of information about the user in the template, for example:

<p>Hi <?php echo $user->name ?>, you haven't logged in since 
<?php echo $user->lastvisitDate ?>.</p>

Remember, this has to be in the template index.php, you can't echo this in an article or module as it needs to be processed at the template level.

Showing Different Content to Different Users

So now we use some CSS...

In our head we put:

<?php
if( !$user->guest ): ?>
<style type="text/css">
.logguest {display:none;}.logmember {}
</style>


<style type="text/css">
.logguest {}.logmember {display:none;}
</style>
<?php endif; ?>

Now, anything I want only guests to see, I surround with a class of logguest, anything I want only a logged in user to see I put a class of logmember. More importantly, because its now CSS, I can put it in a module or article!

For example:

<p class="logguest">
Dude, register already!
</p>
<p class="logmember">
Hi <?php echo $user->name ?>, you haven't logged in since
<?php echo $user->lastvisitDate ?>.
</p>

One thing to bear in mind, the content is still there, but its just not being shown via CSS, so Google will still see it (for example). Its a crude, but simple technique.

Other Content Hiding Options

If you need more control over what content is shown to what users, check out David Towers excellent review of JoomiHide, Ninja Access - JACL and RokAccess

Other User Object Variables

The full list can be found on the Joomla wiki:

  • id - The unique, numerical user id. Use this when referencing the user record in other database tables.
  • name - The name of the user. (e.g. Vint Cerf)
  • username - The login/screen name of the user. (e.g. shmuffin1979)
  • email - The email address of the user. (e.g. This e-mail address is being protected from spambots. You need JavaScript enabled to view it )
  • password - The encrypted version of the user's password
  • password_clear - Set to the user's password only when it is being changed. Otherwise, remains blank.
  • usertype - The role of the user within Joomla!. (Super Administrator, Editor, etc...)
  • gid - Set to the user's group id, which corresponds to the usertype.
  • block - Set to '1' when the user is set to 'blocked' in Joomla!.
  • registerDate - Set to the date when the user was first registered.
  • lastvisitDate - Set to the date the user last visited the site.
  • guest - If the user is not logged in, this variable will be set to '1'. The other variables will be unset or default values.

This article has an SEO score of 100% and content, joomla, users, and show are emphasized within the content and are considered Primary Keywords.
Find out more about Scribe for Joomla


blog comments powered by Disqus
 

Joomlashack University!

If you want online classes delivered to the comfort of your own home, then you need to join Joomlashack University - an affordable online Joomla training course taught by the world's leading experts in Joomla education.

If you are struggling with Joomla and want expert help, then fill out the form below. We'll send you some more information and you'll be first in line join the easiest and fastest way to learn Joomla.



Get Joomla Tips by RSS or Email

 

Joomla 1.5 BookGet the Joomla 1.5 Book

Get the #1 best-selling about Joomla 1.5 from Amazon and free access to the companion site, joomlabook.com.