2

I want to know if there is code to hide a button when logged in.

I can hide a module/menu item by using the built in ACL features, but how can I do it from a code perspective.

1 Answer 1

4

A simple way is to check if the user has an id - if so it means the user is logged in. If id = 0 then it's a guest user and you can show the button as below:

$user = JFactory::getUser();

if($user->id == 0)
{
    // Code to run for non-logged in users.
}


Update:

You can find some examples on how to accomplish various conditional tasks using the user info in these JSE Q/A:

7
  • 2
    if you just want to use some short code in the text editor (so as not to use code as in the above answer), there are plugins that'll let you do this sort of thing. One such plugin is Content Filter by Function90 (there's a free version too). I find it useful to just hide elements right in the article editor.
    – Brian Peat
    Commented Jan 2, 2017 at 4:38
  • @BrianPeat I suppose I could use Regular Labs Snippets too, can they do this?
    – Eoin
    Commented Jan 3, 2017 at 18:01
  • @FFrewin what if I wanted to choose a specific user group? I'm assuming I can do something similar to the above but instead of using id use the user group?
    – Eoin
    Commented Jan 3, 2017 at 18:02
  • Where would I find information like this? I tried J Docs but it's useless. I found lots of deleted pages.
    – Eoin
    Commented Jan 3, 2017 at 18:12
  • 1
    @Eoin: I updated my answer
    – FFrewin
    Commented Jan 4, 2017 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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