Show a different menu depending on whether a user is logged in or not

(23 votes, average 3.70 out of 5)

One of the most common uses for MetaMod is to show a different Joomla menu to users once they are logged in. E.g. the menu for non-logged-in users may contain a “Log in” menu item, and the one for logged in users might have a completely different selection of menu items.

First, set up your two menus. Something you should be concerned about is duplicate content. That is, if you have any identical menu items in both menus, then unless you take steps to prevent it, you will end up with two different URLs pointing to the same page. Search engines like Google do not take kindly to this, and may rank the two almost-identical pages much lower than they would rank if there were only one page.

The way to address this is to set up the first menu normally, then as you are setting up the second one, create menu items in it as “aliases” to the menu items in the first menu. In this way they should end up with the same URL no matter if you clicked on the first menu, or clicked on the corresponding item in the second menu.

With that in mind, on with the recipe:

if ( MM_LOGGED_IN ) return 101; /* the module id for logged-in users */
else return 102; /* the module id for non-logged-in users */

You can also be more creative with this, and use different menus (or any type of module) for different user “groups”, e.g. Author, Administrator, Publisher etc.

The different constants to use are listed at the bottom of the configuration page, but here are some examples:

if ( MM_USER_MANAGER or MM_USER_EDITOR ) return 101;
else return 102;

Display the menu for everyone except logged-in super-administrators:

if ( ! MM_USER_SUPER_ADMINISTRATOR ) return 101;

If you have custom groups which you set up with 3rd-party components, then you need to specify them in a slightly different way:

if ( $user->usertype == "MyCustomGroupName" ) return 101;

See also: this page about making rules based on user and group information.