3

How can I execute a different code in my plugin depending on if the user is logged in or not in Joomla? Basically I want to do this:

<?php
if (loggedin) {
   // Do something 
}
else {
   // Do something else 
}
?>
5

I usually do something like this:

<?php
$user = JFactory::getUser();
if ($user->guest) {
    // User is not logged in
        } else {      
    //User is logged in
        }
?>

Take a look at the Joomla Documentation.

5

You can either use this:

$user = JFactory::getUser();
if($user->id != 0)
{
    // you are a member
}
else 
{
    // you are a guest
}

or this:

$user = JFactory::getUser();
if($user->guest)
{
    // you are a guest
}
else 
{
    // you are a member
}

Bother are the same with most likely nano seconds in performance, in regards to speed.

4

Besides the direct programmatic method to conditionally show different contents (JFactory::getUser()->guest), there's an alternative to create a Guest group to identify this specific set of users; in this way, the content can be managed either at the CMS level with the regular administrative tools or in a plugin with Access Control List controls:

How do you hide something from logged in users? http://docs.joomla.org/How_do_you_hide_something_from_logged_in_users%3F

enter image description here

Associated routine to check if the user belongs to the Guest group:

$user = JFactory::getUser();
$groups = JAccess::getGroupsByUser(user->id, false);
if (in_array(MY_GUEST_GROUP, $groups))
{
   echo 'Only visible for guests';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.