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).
$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;
?>
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?
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
Hi Chris,
Another possibility is that the settings for the registered user menus are not correct. Maybe you’re using a wrong condition?
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.
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;
…great solution by skyrun! (…the direct method)!
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 :)
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.
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!
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;
I have used the following on my website and it works perfectly. Needed 2 Jumi article for a 2 language website.
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);
Hi Jean-Yves,
Thanks for sharing your code with us!