Home >> Blog >> Hiding Joomla! modules from registered users

Wednesday, 13 May 2009 11:04

Hiding Joomla! modules from registered users

Written by  Nicholas K. Dionysopoulos
Rate this item
(3 votes)

Sometimes there is a need to hide Joomla! modules from registered (non-guest) users. For example, we may have a "please register" banner rendered by a module, which makes it rather pointless to be shown to registered users. Unfortunately, there is no such functionality in Joomla! 1.5.x. Instead of groing old waiting for Joomla! 1.6 to arrive, you can use a viable workaround.

 

Concept

For this recipe you'll need Jumi, the all-purpose arbitrary code inclusion solution for Joomla!. Jumi allows you, among other things, to easily create modules which run a custom PHP snippet. It's like having the customization power of Drupal on your Joomla! site. The concept of our recipe is to create a Jumi-powered module which will only load our "real" module – in fact, a module position – if and only if the user is a guest (not logged in). If you haven't already done so, please download and install Jumi.

Creating a faux module position

The first step is to create a "faux" module position. I call it like that because it's not a "real" position, i.e. it isn't actually used as-is by your site's template. It's more of a "proxy", used internally in our solution. Creating it is a piece of cake.

Start by creating, or editing, the module you want to make toggleable. In the module editor, there is a combo box labelled "Position". Intead of using the drop down list, type in a name of your liking. In my example I've used faux_example.

If you need multiple modules to be shown/hidden on the same place in the page, depending on wether the user is logged in or not, repeat this process for any number of modules. For instance, you may have a Custom HTML module and a Banner module on top of it to be shown/hidden at once. Remember to use the same Position parameter!

Creating the "toggle" module

The "toggle" module is the one which controls if the modules in the "faux" module position get shown or not. To create this, go to Joomla!'s Module Manager and create a new module of the type Jumi. In the left-hand column, set the following parameters:

  • Show Title: No. We don't want to see a title if the user is logged in, so leave this blank.
  • Position: Choose where you want the module(s) to be shown, e.g. left.

In the right-hand column, inside the "Code written" text area, enter the following code:

<?php
$position = 'faux_example';
$user =& JFactory::getUser();
if($user->guest)
{
$document = &JFactory::getDocument();
$renderer = $document->loadRenderer('modules');
$contents = '';
echo $renderer->render($position, array('style'=>'xhtml'));
}
?>
 

Just replace the faux_example text with your own faux position's name. Publish the module. Now try viewing the page where this module is placed as a logged out and then as a logged in user. See? It was easy!

Last modified on Saturday, 03 April 2010 12:22

17 comments

  • Comment Link Nelson Friday, 09 September 2011 19:44 posted by Nelson

    Download Advanced Module Manager from Joomla extensions page. This gives you excellent control of modules.

  • Comment Link Nicholas K. Dionysopoulos Sunday, 31 July 2011 20:28 posted by Nicholas K. Dionysopoulos

    Hi Joe, This trick works with any and all modules in Joomla! 1.5 as long as you follow these instructions to the letter.

  • Comment Link Joe Tuesday, 26 July 2011 09:46 posted by Joe

    Tried using this with the cb login module. It's not showing.

  • Comment Link Jean Roxim Monday, 25 April 2011 17:00 posted by Jean Roxim

    This help me with one of my client, I did resolve his needs using this hack.

    I am new in Joomla and I wonder if someone can help me how to show Weather using a plugin? Do you know a good plugin or module that can show weather for at list 4 cities in a little space.

    Thanks a lot!

  • Comment Link Webtest Monday, 14 February 2011 05:09 posted by Webtest

    WOOOOWWWWWWWWW............ worth it man. It worked great.

  • Comment Link eli napchan Wednesday, 22 December 2010 01:40 posted by eli napchan

    When hiding a mainmenu module id did not use the correct CSS to display the menu title and items when showing the menu.

    Menu position (original): left
    Joomla: version 1.5.20
    Template: biz_blue_ii

  • Comment Link binod Thursday, 16 December 2010 09:38 posted by binod

    This worked like a charm and without any error. Right on the money! Thanks a bunch.

  • Comment Link Clint Thursday, 02 December 2010 21:11 posted by Clint

    Fantastic. An elegant solution indeed. Thank you.

  • Comment Link Mark Simpson Wednesday, 29 September 2010 13:42 posted by Mark Simpson

    Or you could just use the Ninja Access module: http://ninjurl.com/929B10

    /shameless plug :P

  • Comment Link Mark Simpson Wednesday, 29 September 2010 13:41 posted by Mark Simpson

    Or you could just use the Ninja Access module: http://ninjurl.com/929B10

    /shameless plug :P

«StartPrev12NextEnd»

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.
All comments are auto-published. Unless you are a spammer, violate any laws or ask me to unpublish your comment, I won't unpublish any comments. Feel free to say anything you like.