How To Create A Logout Menu Item In Joomla?

Sometimes, even the seemingly easiest things are very complicated to in Joomla. One of these easy, yet complicated things to do, is creating a Logout menu item in Joomla. We had this request several times so we thought it would be helpful to share how to do it.

Note that the below applies to both Joomla 1.5 and Joomla 1.6.

Step 1 – Install Jumi

The first thing that you need to do is to install Jumi, an extension that allows you to have PHP code in your text (and will have that PHP code processed as well). Jumi is compatible with all versions of Joomla.

Step 2 – Create article

Create a new article (let’s call it “logout”) and paste the following code into a Jumi:

<?php
$return = base64_encode('http://www.example.com');
$url = JRoute::_('index.php?option=com_users&task=user.logout&' . JUtility::getToken() . '=1&return=' . $return);
header('Location: ' . $url);
exit;
?>

Note: Swap ‘http://www.example.com’ with the URL that you would like users redirected to after log out.

Step 3 – Create menu item

Create a new menu item with a Menu Item Type of “Single Article” and select the “logout” article. You will also want to set the Access of this menu item to your Registered group or a similar group of logged in users so that only logged in users can see your logout link.

That’s it, you’re done! You now have a logout link in you menu!

If you think that the above is a bit over your head, then feel free to contact us and we’ll be glad to help you. We will charge you for an hour’s work (please check our fees).

<?php
$return = base64_encode(‘http://www.example.com’);
$url = JRoute::_(‘index.php?option=com_users&task=user.logout&’ . JUtility::getToken() . ‘=1&return=’ . $return);
header(‘Location: ‘ . $url);
exit;
?>
13 Responses to “How To Create A Logout Menu Item In Joomla?”
  1. Comment by Chris — August 18, 2011 @ 9:30 pm

    Working on a site for a customer, I am creating a front-end editing portion. Using Joomla 1.6 I want to make a logout button. Using your method is great. Suddenly today, the “registered user” menus will not go away. I even opened a fresh browser, boom. There is the menu.

    Any thoughts?

  2. Comment by Fadi — August 19, 2011 @ 2:07 am

    Hi Chris,

    Could it be that you’re using Joomla caching and you haven’t cleared your cache? Many times when your changes don’t reflect properly this could be the problem. See: http://www.itoctopus.com/my-joomla-changes-are-not-showing

  3. Comment by Fadi — August 19, 2011 @ 2:09 am

    Hi Chris,

    Another possibility is that the settings for the registered user menus are not correct. Maybe you’re using a wrong condition?

  4. Comment by skyrun — November 7, 2011 @ 6:35 pm

    the following works better for me (other didn’t work…) note option=com_user (not com_users) and i substituted ‘index.php’ for the whole url to make it more generic and work on any site. without index.php, you have to hard-code the site AND path. (http://www.skyrun.com/test for example) so this just makes it more generic… one less thing to change in the example.

    NOTE ALSO that instead of creating an article and installing the Jumi… you can just create a menu item as an external link with the link:

    index.php?option=com_user&task=logout&return=aW5kZXgucGhw

    (aW5kZXgucGhw is base64 for index.php… so change as necessary using http://www.motobit.com/util/base64-decoder-encoder.asp)

    this way… no article, no Jumi.

  5. Comment by skyrun — November 7, 2011 @ 6:37 pm

    re-posting my lines of code (didn’t post with php tags around it first time)

    $return = base64_encode(‘index.php’);
    $url = JRoute::_(‘index.php?option=com_user&task=user.logout&’ . JUtility::getToken() . ‘=1&return=’ . $return);
    header(‘Location: ‘ . $url);
    exit;

  6. Comment by kstampo — February 11, 2012 @ 3:11 am

    …great solution by skyrun! (…the direct method)!

  7. Comment by Sebastien — July 20, 2012 @ 5:16 pm

    Thank you… it’s the first time I try Jumi and I already think about many usefull application to this great extension.

    I noticed that you can also create a menu item to a Jumi Application but I had an issue that I want to share:

    If you create a submenu item to a Jumi Application and SEF urls are turned on it will not work unless you disable the JumiRouter plugin.

    Sebastien :)

  8. Comment by Fadi — July 21, 2012 @ 8:50 am

    Hi Sebastien,

    Have you tried to change the order of your Joomla plugins? When you’re in the “Plugin Management” page, there’s a way you can change the order of plugins. By doing this, you will tell Joomla which plugin should run before which.

  9. Comment by Royce — April 30, 2013 @ 3:20 pm

    Here is another example that worked for me in Joomla! 3.1. Instead of JUMI, I used the nonumber Sourcerer plugin to get PHP in an article
    <?php
    {source}

    {/source}
    ?>

    The trick was to use JSession::getFormToken() and to set the ‘false’ parameter to JRoute. Otherwise it would convert the & to amp; and it would fail the token check because the token had the amp; in front of it!

  10. Comment by Royce — April 30, 2013 @ 3:24 pm

    Last try, ignore/remove 2 previous comments.

    $return = base64_encode(‘http://example.com/index.php’);
    $url = JRoute::_(‘index.php?option=com_users&task=user.logout&’ . JSession::getFormToken() . ‘=1&return=’ . $return, false);
    header(‘Location: ‘ . $url);
    exit;

  11. Comment by Jean-Yves — February 24, 2014 @ 6:43 pm

    I have used the following on my website and it works perfectly. Needed 2 Jumi article for a 2 language website.

  12. Comment by Jean-Yves — February 24, 2014 @ 6:47 pm

    It appears that the code did not display…

    Changed $return = base64_encode(‘http://example.com/index.php’); to
    $myabsoluteurl=JURI::base();
    $return = base64_encode($myabsoluteurl);

  13. Comment by Fadi — February 26, 2014 @ 8:27 am

    Hi Jean-Yves,

    Thanks for sharing your code with us!

Leave a comment