How-To Categories
Recent How-To's
News Feed E-mail
Login
| Making module positions viewable only by unregistered visitors |
|
|
|
| Written by Cory | |
| Tuesday, 12 September 2006 | |
|
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: <?php if ( $my->id ) { // Code to generate if visitor is a registered user } else { // Code to generate if visitor is not a registered user } ?> 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. <?php if ( !$my->id ) { mosLoadModules( 'guests_only_position', -2 ); } ?> 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.
Set as favorite
Bookmark
Email This
Hits: 12343 Trackback(0)
Comments (37)
![]()
Dan: ...
Great little hack, thanks for sharing. Worked like a charm for me
![]() 1 report abuse
vote down
vote up
December 27, 2006
Votes: +0
Manu: ...
Very simple & very usefull !
Thanks ![]() 3 report abuse
vote down
vote up
January 17, 2007
Votes: +0
rick dias: ...
very nice just what i needed, thx
![]() 4 report abuse
vote down
vote up
January 30, 2007
Votes: +0
Ed: ... http://everyminutesucks.com
Awesome! I have just started using Joomla and have been looking for this for a long time!! Thanks Man!
6 report abuse
vote down
vote up
February 22, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
How about putting this in the head then?
I have this in a test template and can be used for just about anything. The main idea is to have different css for public and registered users. The php files just add css to the head. Or you can simply echo a link to the appropriate stylesheet. Including phph files however also allows me to perform different php for public and registered. Now it's just been used for emphasizing certain divs for the public, while visually moving them to the background for registered users. 7 report abuse
vote down
vote up
March 03, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
O no code allowed? Okay pseudo code placed in the template's head
if logged in include public.php else include registered.php 8 report abuse
vote down
vote up
March 03, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
Yes, I got it working. Thanks for sharing.
I was initially trying to get working within a module itself, but that didn't work. Now I got it in the template file and just needs combining with my 'dynamic css' scripts. Calculates container divs sizes and positions based on browser width (js/cookies). You can probably also hide/show things completely for public/registered with css like "display:none" ... but for now have no use yet for the latter. But I'm sure I'll find plenty more uses for different php (and css) for public and registered. 10 report abuse
vote down
vote up
March 03, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
True. Was just thinking out loud. You're right.
In some cases I just want different css, let's say a large interstatial for public and a 'background' banner for registered. Or in other cases, let's say a splash screen or take-the-tour- div on first visit, you might be better off with cookies. In some cases you might be better off with doing the if logged in loop in the body, as to show or hide something alltogether ... Or in some cases you might combine it all. As to what originally raised this issue for me was different css for public and registered. Now the next ultimate cool thing would be if I could assign different css (or php or java or anything else for that matter) based on user-type. Different css for each different userlevel, including custom levels (ie using JACLPlus). That ... well that would be ultimaltely cool ![]() 12 report abuse
vote down
vote up
March 03, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
Btw Cory, compliments all round to you. And thanks!! Had a quick glance here and there and found some very useful little things. You are a true master of getting things done the simple way. Furthermore what an excellent choice of selection.
"Long live Sir Cory" !!! 13 report abuse
vote down
vote up
March 03, 2007
Votes: +0
Dylan: ... http://www.rozemandarijn.nl
Ah, yes indeed Cory. A mambot. I hadn't thought bout that. :eureka: and :thumbsup:
For now I can do with what I have. Primary desire was to absolutely position all divs and absolutely define all containers. And for some have a fixed width or height, ie a header, pics in a photo gallery. So I started with JS for the browser width and cookies to get it to php. Then include a settings file where I can 'configure' min width, max width, fixed sizes &such. Then calculate all positions and sizes simply by going if module exist move and or narrow the next div so many pixels down or to the right. Took some time since I'm relatively new ... well at least compared to you ... but got it. But I got the hang of it. Next I just wanted to whisper instead of shout a specific (html) module once the user logs in. No need to shout to logged in users. I assume they know what the site's about. That's all. And since I already had the dynamic css for positioning/sizing I thought I'd just plugin that little bit extra. Whisper vs. shout. After all, only a very little bit of extra css. Sorry to go on so long but hey you did say ... I will leave it up to you to come up with more creative ways to use it Basically I just see the following loop which can be used to serve up content as well as presentation, shout vs. whisper if not logged in use javascript/cookies to determine everything you can/need add public rules add context-relative rules // ready to serve up content, presentation, ads ... elseif logged in can still use js/cookie // ie browser width add registered rules // loop for several components, ie cb, e-commerce stuff, tracking stats, acl foreach extension ( if (userinfo not empty (get userinfo) ) add context-relative rules // ready to serve up content, presentation, ads ... endif Serve it up. Enjoy your meal. So you can do anything with this if else loop, just set off a set of rules and logic to determine what content to show and lest not forget -how- to show it. Can be used bot for helping the user. ie On an automobile site, to determine what type of car the user is into (convertible|suv|sports) you might be able to use his profile, his history, something set from doing a poll (i drive a...) or just about anything else to serve up content weighing it accordingly to the users info. To enhance user-friendliness: Hey you might like this article, forum post, you might even like these members who also drive a fabulous audi convertible just-! like-! you-! ... as to enhance ads-friendliness/banner targeting: hey, you might like this banner And having to do some dynamic css anyway, you might as well throw in some random rules as well, i.e. to rotate font-color used for text-ads? Just a thought. Hey, why settle for just the related items module? In short, what a nifty little trick .. that if logged in loop. And personally, I just have to watch out that I don't totally go hogwild. 15 report abuse
vote down
vote up
March 04, 2007
Votes: +0
Vince Cardillo: ... http://www.vincecardillo.com
THANK YOU! I just read this article after poking around forever in the Joomla documentation. I love Joomla but, seriously, their documentation sucks sometimes. I've been trying to figure out this little trick forever!
Thanks so much. 16 report abuse
vote down
vote up
April 11, 2007
Votes: +0
paul: ...
What if i want to hide a specific module from registered users (not the entire module position)... can it be done ?
18 report abuse
vote down
vote up
April 28, 2007
Votes: +0
max: ...
Is it possible to change the whole look and feel of your web page depending on which page/section of a site you are on
20 report abuse
vote down
vote up
May 13, 2007
Votes: +0
dawn: ... http://www.sidewalkdesigns.com
Sort of confused by this - does anyone want to do this for me - willing to pay.
![]() 21 report abuse
vote down
vote up
June 11, 2007
Votes: +0
Jamesg: ... http://www.workingtechnologyparthers.com
Cory,
Thanks! This works well!!! Max, try if a switch on the Itemid if Itemid = x do what you want to do else what else you want to do endif 22 report abuse
vote down
vote up
July 19, 2007
Votes: +0
Henk: ... http://www.4success.co.nz
max,
yes - to change site based upon section view : http://www.joomlatribune.com/joomla/sections-with-different-templates.html uses multiple templates per site and assigns per menu item. in action on my site.... ![]() 23 report abuse
vote down
vote up
August 31, 2007
Votes: +0
lars: ... http://www.ameridane.biz
Cory,
That's a very useful little hack; you might want to add that $my->id actually holds a value that tells you what kind of user is logged in -- that allows you to further slice and dice your modules or content on the fly. I've got a client site where modules need to appear and/or disappear depending on whether you're an anonymous visitor, a registered user, or an "insider" (e.g. an intranet kinda deal). The two levels of registration maps to "registered" and "special" in the user setup and can be sniffed out via the value of $my->id 24 report abuse
vote down
vote up
September 20, 2007
Votes: +0
Brian: ...
Where do I add this code? I created a "Register now" module and want to hide it from registered users. So, where do I enter your code? Thanks!
25 report abuse
vote down
vote up
October 01, 2007
Votes: +0
Brian: ...
Cory, sorry to be dense, I hate answer questions over and over, and I'm sorry to ask you to, but I have to ask again because I'm not sure I understand.
I created a register module, basically an additional module saying "Register now to access this and this..."I have the module placed on the right hand side. But, registered users see this too and they end up clicking it and getting the "not authorized" thing. I don't want registered users to see this particular module. I don't know where in the template to add this code? So if I'm using rhukII default template, would I just edit the html through joomla backend and stick this wherever? I tried that and it didn't work. I'm not sure if I incorrectly named the module or what, but I couldn't get it to work. Thanks Cory! 27 report abuse
vote down
vote up
October 01, 2007
Votes: +0
locoman: ... http://www.whitewedding.ro
HOW TO LOAD A MODULE DEPENDING OF THE LANGUAGE
29 report abuse
vote down
vote up
October 20, 2007
Votes: +0
locoman: ... http://www.whitewedding.ro
HOW TO LOAD A MODULE DEPENDING OF THE LANGUAGE
30 report abuse
vote down
vote up
October 20, 2007
Votes: +0
Brian: ...
I got it working, pretty simple once you figure out the template. Thanks! One of the best hacks for Joomla!
32 report abuse
vote down
vote up
November 01, 2007
Votes: +0
John Scott: ... http://www.SavvyMart.com
Hi Cory,
Quick question - does the code test whether they are a registered user or whether they are logged in? I am wanting to show/hide instructions (within content), ie "You must be logged in to download this file" depending upon whether the user is logged in. I found a mambot moshidefrom, but it required MAL component to work. I will see if I can adapt your "argument". 34 report abuse
vote down
vote up
January 02, 2008
Votes: +0 Write comment
|
| < Prev |
|---|












... but got it. But I got the hang of it.
