Making module positions viewable only by unregistered visitors

Here is a trick for making your module positions viewable only by unregistered visitors.

There are some powerful programming objects in the Joomla! API that are available to template designers. One such object is the user object, which is accessible with the php variable $my.

If you want to determine whether or not a visitor is a registered user, you simply need to add the following code:

 

This code basically just says "If this visitor has a user id, then generate the first set of code. Otherwise, generate the second set of code."

We can now take this idea one step further. Let's use this trick we've just learned to tell our template not to display a module position to registered users.

 

Notice the "!" before $my in this example. You interepret this by adding the word not. So this code basically says, "If this visitor does not have a user id, then load the modules that are assigned to the module position 'guests_only_position'." (Please note that "guests_only_position" is not a standard module position in Joomla!. I only used it for the sake of this example.)

This trick is useful if you have items that you do not want to show your registered users. For example, you might have a banner inviting visitors to join your site. Your registered users do not need to see this banner, so you can use this trick to hide it from them.

There are a number of possible uses for this trick in your Joomla! template. I will leave it up to you to come up with more creative ways to use it.