Show Joomla module to registered users only using code

Show Joomla module to registered users only using code

Most of case you may find that you want to spilit the article content or Joomla page for your logged-in users(registered users) or unregistered users. In this situation below code will helping to separate content to show only for loggin-in user and non-login users(guests).

On the AppClave site, for example, the "Sign In" or "Log In" button in the top right changes to a "My Account" link once the user login is complete(Not for guest visitor).

Joomla User Object Code:

The first thing we'll need to do is define a Joomla variable:

$user = JFactory::getUser();

Which will execute the code in the if statement only for registered users.

$user = JFactory::getUser();
if($user->id > 0) {
// code will display for the registered users only
}

For example you can add Joomla module code like below for display content to registered users only

$user = JFactory::getUser();
if($user->id > 0) {
//
}

Note: Please remove '//' before above 'jdoc' code

If we want to display the content for unregistered users (guests) only, we can check if the user id is equal to 0 instead.

$user = JFactory::getUser();
if($user->id == 0) {
// code will display for the guest users only
}