Scenes from New Hampshire Scenes from New Hampshire Scenes from New Hampshire Scenes from New Hampshire
Joomla! Registered User Group
Find us on Facebook!  Join our Meetup Group  Join our Google Groups Mailing List  Follow us on Twitter!

Favorites Create PDF Email Print

Hiding Modules When a User Logs In

Author:
Justin Herrin
Date Added:
Saturday, 17 October 2009
Last Revised:
Tuesday, 20 October 2009
Hits:
1139
Rating:
 
Vote for This:
Good - Bad
Favored:
0 Favor

Item Details

We all know Joomla is pretty good at hiding things from the public, and then exposing them once the user logs in. However, what happens when you want to hide a module position, for example, when the user logs in. I had this problem with a customer's site. I put in a "Subscribe" module for people to sign up for their documentation but once people registered I didn't want it annoying them. Well, it turns out that it's only a few lines of code in the main template file to hide it.

To hide a module position all we have to do is work out if the user is logged in. The Joomla Framework API (that's just a fancy name for some of the code that runs the Joomla web site) has a way of getting the user information. All we need to do is check if the User ID is not zero and then we will know if they are logged in. Let's see how this is done.

Open up the main template index.php file for your active template. It will be something like:

/templates/ja_purity/index.php

You should already be familar with how Joomla displays a module position (with one of those <jdoc: ... /> tags). Find the module position that you want to hide and then wrap it in the following code:

<?php
// Get the user object
$user = &JFactory::getUser();
// Now work out the User ID
$userId = $user->get('id');
// Now hide the module if user is logged in
if ($userId == 0) :
// Now we break out of the PHP tags and display the JDOC tag to include the module
?>
<jdoc:include type="modules" name="public-right" style="xhtml" />
<?php endif; ?>

You will probably want to create a special module position. In this case I've called it "public-right" to indicate it will display in the right column, but only for the public user. When you log into your site, you should find that any modules in the "public-right" position will disappear.

This is a very simple technique but it can have a profound effect on the "feel" of your web site.

Category

What is JUGNH?

Joomla! User Group New Hampshire is a group of Joomla enthusiasts, from beginners to professionals, who meet monthly to share new projects, problem solve, network and make new friends. The location of our meetings alternates between the Seacoast and Manchester areas.