Hack: Hide any module for logged in users ONLY

Your code modifications and patches you want to share with others.
User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Hack: Hide any module for logged in users ONLY

Postby phlux0r » Thu Nov 16, 2006 5:54 am

Updated 01. May 2007

This hack has moved from the mosLoadModules function to the &initModules function since that one is being called first by all the other functions. This will allow for column collapsing if the module is hidden and no other modules are published in the position.

Credits go to BradWaite for pointing out to modify &initModules to collapse modules.

You only need this hack in one place, in function: &initModules()!!!!

For a more elaborate hack allowing hiding of modules for certain user types see my post further down: http://forum.joomla.org/index.php/topic ... #msg795120


[This hack is done in J! 1.0.12]

I had a situation where i had a menu that I wanted to replace with the User menu once a user was logged in. In other words I wanted to hide that public menu when a user logged in.

This is such a simple hack that I don't see why this couldn't be part of the core. It involves adding a parameter to the module's xml file (in my case /modules/mod_mainmenu.xml but you can add this parameter to any module you want to hide.

Simply insert this in the position you want in the module.xml file:

Code: Select all

      <param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>


Then open up: /includes/frontend.php and find the function &initModules() around line 72. In there you should see this code:

Code: Select all

   foreach ($modules as $module) {
      $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
   }

Insert immediately before the line with $GLOBALS[... the following code:

Code: Select all

               // HACK: added code to check if module should be shown when a user is logged in
      $params = new mosParameters( $module->params );
      if (!$params->get('show_registered', 1) && $my->id) {
         // simply continue the loop without outputting this module
         continue;
      }
      // --- end HACK ---

This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.
Last edited by phlux0r on Mon May 14, 2007 9:35 pm, edited 1 time in total.

steve4j
I've been banned!
Posts: 143
Joined: Sat Sep 03, 2005 3:37 pm

Re: Hack: Hide any module for logged in users ONLY

Postby steve4j » Thu Nov 16, 2006 10:16 pm

you sir, deserve a medal ( or a swift kick in the pants for not doing this earlier!) good job!

very elegantly done!

User avatar
Robin
Joomla! Master
Joomla! Master
Posts: 15753
Joined: Thu Aug 18, 2005 10:41 am

Re: Hack: Hide any module for logged in users ONLY

Postby Robin » Sat Nov 18, 2006 6:11 am

Moderator note; moved from FAQ >> 3rd Party, Hacks/Patches

QuickSync
Joomla! Intern
Joomla! Intern
Posts: 78
Joined: Thu Aug 18, 2005 3:36 am

Re: Hack: Hide any module for logged in users ONLY

Postby QuickSync » Sat Nov 25, 2006 10:38 am

Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D

User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby phlux0r » Sun Nov 26, 2006 1:49 am

Hi quickwolf,

sorry, but I didn't need that kind of functionality so I haven't looked at hacking that.

steve4j
I've been banned!
Posts: 143
Joined: Sat Sep 03, 2005 3:37 pm

Re: Hack: Hide any module for logged in users ONLY

Postby steve4j » Sun Nov 26, 2006 6:08 pm

QuickSync wrote:Hi phlux0r,

Do you have the same implementation for menu items?

I mean, some menu items must be visible to public but should disappear once logged on...

Thank you!

quickwolf ;D


it would seem to me, that if you place all your menu items that should be visible for PUBLIC but invisible for LOGGED IN, you could (should) put all those menu items together in one menu module, then apply the Logged In/out hack to that module.

toddrip
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Dec 07, 2006 6:09 am

Re: Hack: Hide any module for logged in users ONLY

Postby toddrip » Wed Jan 24, 2007 12:57 am

This works perfect!!!!!!!!!  Thanks  ;D

User avatar
eskimex
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Mon Nov 06, 2006 10:18 am
Location: Jerusalem, Israel
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby eskimex » Sun Jan 28, 2007 8:37 am

can i use this for 1.0.12 ?

User avatar
eskimex
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Mon Nov 06, 2006 10:18 am
Location: Jerusalem, Israel
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby eskimex » Sun Jan 28, 2007 8:38 am

toddrip wrote:This works perfect!!!!!!!!!  Thanks  ;D


toddrip,

are you using 1.0.12 ?

User avatar
Chinaman
Joomla! Guru
Joomla! Guru
Posts: 575
Joined: Sun Aug 21, 2005 8:46 am
Location: Perth, Western Australia
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby Chinaman » Sun Jan 28, 2007 11:42 am

I am using it for 1.0.12 and works fine so far for menus. Have not tried it for other modules
Joomla! - enjoying every minute of the journey!

User avatar
eskimex
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Mon Nov 06, 2006 10:18 am
Location: Jerusalem, Israel
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby eskimex » Sun Jan 28, 2007 12:31 pm

thanks for the response.

what i need this function for is i have a "become a member" link, and i didnt want it to show to people logged in... cause thats kinda dumb. AND, i could use the space for something else.
so this is what i did.

Code: Select all

if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


very simple. i didnt use the hack. this was good enough. and i am more comfortable writing it this way.

User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby phlux0r » Sun Jan 28, 2007 9:42 pm

You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code: Select all

if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code: Select all

if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }

User avatar
eskimex
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Mon Nov 06, 2006 10:18 am
Location: Jerusalem, Israel
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby eskimex » Mon Jan 29, 2007 7:32 am

phlux0r wrote:You should use the sefRelToAbs() function for URLs so it will work with any SEF component and SEF off. You code only works when you have native SEF enabled... so instead of:

Code: Select all

if (!($my->id))
{    echo "<a href='/component/option,com_comprofiler/task,registers/' class='help'>Become a member for FREE! </a>"; }


write:

Code: Select all

if (!($my->id))
{    echo "<a href='<?php sefRelToAbs("index.php?option=com_comprofiler&task=registers"); ?>' class='help'>Become a member for FREE! </a>"; }




thanks for the tip. i put this code in my template. and im not sure why but i cant get your code to work.
when your code is in my template, i get an error.
unexpected "
so i replaced " with '
but now the php doesnt get read. how do i write this for my template?
thanks!
nate

audioindonesia
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Fri Jan 27, 2006 12:29 pm
Location: Jakarta
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby audioindonesia » Wed Mar 28, 2007 5:15 pm

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/mysite/public_html/smunsa/templates/themeku/index.php on line 11
Audio Indonesia
Joseph Manurung

Good Leaders are Good Followers

User avatar
Comenius
Joomla! Intern
Joomla! Intern
Posts: 90
Joined: Sun Nov 06, 2005 12:57 am
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby Comenius » Thu Mar 29, 2007 10:47 pm

The module hack is just perfect.  Thanks so much!!  :D
Learn English Online at Open English World: http://www.OpenEnglishWorld.com
Teach English as a Second Language at ESL Jobs World : http://www.ESLJobsWorld.com

josgall
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Wed Feb 07, 2007 9:16 pm

Re: Hack: Hide any module for logged in users ONLY

Postby josgall » Sat Mar 31, 2007 12:11 am

i've got another good one, to which I have to give phlux0r the credit  8).

How about a hack for "Hide a module when your site is in SSL or on the secure SSL site for payments systems" - here ya go...


Simply insert this in the position you want in the module.xml file:


Code: Select all

      <param name="hide_for_ssl" type="radio" default="0" label="Hide Menu When SSL is active" description="Hide the menu when the SSL is active">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>


Then open up: /includes/frontend.php and go to line 155. You should see this code:

Code: Select all

   foreach ($modules as $module) {
      $params = new mosParameters( $module->params );


Insert immediately after that the following code:

Code: Select all

      // HACK: added code to check if module should be shown when the user is in SSL
      if ($params->get('hide_for_ssl', 0) && substr( $mosConfig_live_site, 0, 8 ) == 'https://' ) {
      // simply continue the loop without outputting this module
      continue;
      }
      // --- end HACK ---



This will work for modules that don't have that parameter defined as it will default to 'display' (or 1) the module when logged in as per normal functionality.

Now you can go into your module configuration and set the radio button to 'No' for the module you want to hide from logged in users.

Enjoy.

bdcomp
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Wed May 17, 2006 6:44 am

Re: Hack: Hide any module for logged in users ONLY

Postby bdcomp » Sat Mar 31, 2007 8:12 pm

Hi,

I slightly changed the second piece of code, because josgall's version didn't worked for me:

Code: Select all

// HACK: added code to check if module should be shown when the user is in SSL
if ($params->get('hide_for_ssl', 0) && ($_SERVER['HTTPS']=='on')) {
// simply continue the loop without outputting this module
continue;
}
// end HACK


Thank you josgall and phlux0r  :)

Boaz

BradWaite
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Tue Sep 05, 2006 8:39 pm

Re: Hack: Hide any module for logged in users ONLY

Postby BradWaite » Sun Apr 08, 2007 9:31 pm

Excellent hack, Phlux0r!

I've made some modifications to the placement so it works with templates that collapse when a module isn't present.

Replace the following code at line 94 of frontend.php

Code: Select all

                foreach ($modules as $module) {
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      



with this:


Code: Select all

                foreach ($modules as $module) {
                        // HACK: added code to check if module should be shown when a user is logged in
                        $params = new mosParameters( $module->params );
                        if (!$params->get('show_registered', 1) && $my->id) {
                                // simply continue the loop without counting this module
                                continue;
                        }      
                        // --- end HACK --
                        $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
                }      


Combine this hack with the mosviewonly/moshideonly mambots from http://www.troozers.com/ and you can really customize your site for registered users.

somos
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Jan 09, 2007 7:39 am

Re: Hack: Hide any module for logged in users ONLY

Postby somos » Sat Apr 21, 2007 6:08 am

This is so usefull that should be in every module and in the default installation. I expected to found it without hacking anything ... it seems a common need for me.

BradWaite
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Tue Sep 05, 2006 8:39 pm

Re: Hack: Hide any module for logged in users ONLY

Postby BradWaite » Sat Apr 21, 2007 5:59 pm

For you unix guys, here's a quick perl script to add this parameter to every module.  Just put this into a file called "addparm" and run "addparm *.xml" from your modules/ dir.

Code: Select all

#!/usr/bin/perl -pi

$addparm = qq|\t\t<param name="@spacer" type="spacer" default="" label="" description="" />\n|;
$addparm .= qq|\t\t<param name="show_registered" type="radio" default="1" label="Show to registered users" description="Show this module when a registered user is logged in">\n|;
$addparm .= qq|\t\t\t<option value="0">No</option>\n|;
$addparm .= qq|\t\t\t<option value="1">Yes</option>\n|;
$addparm .= qq|\t\t</param>\n|;

if( /<\/params>/){
        print $addparm;
}

meekochan
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Mar 08, 2007 10:46 am

Re: Hack: Hide any module for logged in users ONLY

Postby meekochan » Mon Apr 30, 2007 9:31 pm

Hi !

thanks for the great hack! it was EXACTLY what i was looking for, both the first hack and the last hack before the unix one

could you add one more "feature"? I was just wondering if it was possible to hide a certain module only if a super administrator was logged in, instead of any registered user? Or it could technically be any specific level of a user, and when that user is logged in, that module will dissapear and the spot on the template will collapse?

If you could do it, it would be great, if not, it works great anyways!

So thanks in advance!

Miyuki
Last edited by meekochan on Mon Apr 30, 2007 9:33 pm, edited 1 time in total.

User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby phlux0r » Tue May 01, 2007 1:52 am

a) I've updated my original hack so check it out please to see what has changed.

b) Ok, nice challenge meekochan!

This is for Joomla! 1.0.12

Here we go...

To add the possibility of hiding modules for a certain user group (i.e. Authors, Publishers, Managers etc.) this is what is needed:

1) In your /modules/[whatever_module].xml you need to add the following somewhere within the section:

Code: Select all

      <param name="show_registered" type="radio" default="1" label="Show Menu When Logged In" description="Show the menu also when a registered user is logged in">
         <option value="0">No</option>
         <option value="1">Yes</option>
      </param>
      <param name="hide_for_gid" type="list" default="0" label="Hide Module for this User Type Only" description="If above is set to 'No', you can specify which registered user type you want to hide this module for. Default hides the module for any logged in user.">
         <option value="0">Default</option>
         <option value="18">Registered</option>
         <option value="19">Author</option>
         <option value="20">Editor</option>
         <option value="21">Publisher</option>
         <option value="23">Manager</option>
         <option value="24">Administrator</option>
         <option value="25">Super Administrator</option>
         <option value=">19">Author and above</option>
         <option value=">20">Editor and above</option>
         <option value=">21">Publisher and above</option>
         <option value=">23">Manager and above</option>
         <option value=">24">Administrator and above</option>
         <option value="<19">Author and below</option>
         <option value="<20">Editor and below</option>
         <option value="<21">Publisher and below</option>
         <option value="<23">Manager and below</option>
         <option value="<24">Administrator and below</option>
      </param>


2) Then in /includes/frontend.php in function &initModules() on line 73 change the global line to:

Code: Select all

   global $database, $my, $Itemid, $acl;

(added $acl to the variables)

3) Further down in the same function, replace this code:

Code: Select all

      foreach ($modules as $module) {
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


with this code:

Code: Select all

      // HACK: to hide modules for certain groups of logged in users
      $myRealGid = intval($acl->get_group_id($my->usertype));
      // --- end HACK ---
      foreach ($modules as $module) {
         // HACK: added code to check if module should be shown when a user is logged in and is in a particular group
         $params = new mosParameters( $module->params );
         if (!$params->get('show_registered', 1) && $my->id) {
            // check the gid if param set
            if ($params->get('hide_for_gid', 0) != '0') {
               if (strlen($params->get('hide_for_gid')) > 2) {
                  // if we're looking at ranges, extract the comparator and the gid to compare
                  $comparator = substr($params->get('hide_for_gid'), 0, 1);
                  $hideGid = intval(substr($params->get('hide_for_gid'), 1));

                  if (($comparator == ">" && $myRealGid >= $hideGid) || ($comparator == "<" && $myRealGid <= $hideGid)) {
                     continue;
                  }

               } elseif (intval($params->get('hide_for_gid')) == $myRealGid) {
                  // if the user's gid is equal to the parameter, hide the module
                  continue;
               }
            } else {
               // simply continue the loop without outputting this module
               continue;
            }
         }
         // --- end HACK ---
         $GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }


And it's done. You can then set for which kinds of registered users you want the module to be hidden. The parameter list allows you to specify the exact level (i.e. Authors only) or a range like "hide for Editors and above" or "hide for Managers and below" which hides the module for that level user and above/below.

Hope this is useful to some of you, as always, enjoy.

Edit: Just realised that the option: "Registered and below" doesn't make sense (cos it's the same as "Default") so I removed it :).
Last edited by phlux0r on Tue May 01, 2007 9:58 am, edited 1 time in total.

User avatar
Robin
Joomla! Master
Joomla! Master
Posts: 15753
Joined: Thu Aug 18, 2005 10:41 am

Re: Hack: Hide any module for logged in users ONLY

Postby Robin » Tue May 01, 2007 7:04 am

Looks very interesting, I'm sure going to check this out!

meekochan
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Mar 08, 2007 10:46 am

Re: Hack: Hide any module for logged in users ONLY

Postby meekochan » Tue May 01, 2007 7:40 am

Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!

And I tried it, and it works great. Thank you so much!

User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby phlux0r » Tue May 01, 2007 10:05 am

Hi! Thanks for going extra and even adding below and above, that was a great idea. I couldn't ask for more!


Not a problem... it was a nice addition to the hack and not really all to difficult.

If one wanted to be really fancy, one could create a new parameter type for the section say type="acl_group_list" and get it populated with the ACL groups from the database. At the same time making the list multi select (dunno if you could even have multiple values for a parameter - may need some thought) so you could pick which groups to hide the module for. That way this would work even with the JACLPlus extension/hack...

nluk100
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon Feb 05, 2007 9:52 pm

Re: Hack: Hide any module for logged in users ONLY

Postby nluk100 » Wed May 09, 2007 6:17 pm

This is just what I have been trying to sort out for over 2 weeks now since installing my SSL!

Mucho thanks!

jce108
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Fri Jul 14, 2006 4:50 pm

Re: Hack: Hide any module for logged in users ONLY

Postby jce108 » Mon May 14, 2007 7:47 pm

Hey, I am having problems implementing this hack, I can add the radio button to the XML fine but in the frontend.php whenever i change it from
This:

Code: Select all


foreach ($modules as $module) {
$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



To this:

Code: Select all


foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }



The website just displays a blank white screen.
I'm kind of new to coding so any help is greatly appreciated
Last edited by jce108 on Mon May 14, 2007 7:58 pm, edited 1 time in total.

User avatar
phlux0r
Joomla! Guru
Joomla! Guru
Posts: 812
Joined: Thu Feb 09, 2006 1:41 am
Location: Auckland, NZ
Contact:

Re: Hack: Hide any module for logged in users ONLY

Postby phlux0r » Mon May 14, 2007 9:38 pm

Yep, there's a mistake in the hack snippet in the irst post (I fixed it now).

You need to add:

Code: Select all

$params = new mosParameters( $module->params );

before the if statement. So your replacement should look like:

Code: Select all

foreach ($modules as $module) {

// HACK: added code to check if module should be shown when a user is logged in
$params = new mosParameters( $module->params );
if (!$params->get('show_registered', 1) && $my->id) {
// simply continue the loop without outputting this module
continue;
}
// --- end HACK ---

$GLOBALS['_MOS_MODULES'][$module->position][] = $module;
      }

quizzer
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Thu Sep 01, 2005 7:47 pm
Location: Lincoln UK

Re: Hack: Hide any module for logged in users ONLY

Postby quizzer » Tue May 15, 2007 3:28 pm

I am developing a site that uses "Tabs & Slides In Content Items Plugin" from Joomlaworks and I would like to be able to hide a module when one of the tabs is opened.

I was just wondering if a variation of this hack would do the trick?

matrix845
Joomla! Guru
Joomla! Guru
Posts: 927
Joined: Tue Oct 10, 2006 4:09 pm

Re: Hack: Hide any module for logged in users ONLY

Postby matrix845 » Sat Jun 23, 2007 2:29 pm

@quizzer
tabs and slides is for a content not for a module  ;)


Return to “Core Hacks and Patches”

Who is online

Users browsing this forum: No registered users and 1 guest