How to Check for the Default Page in Joomla 2.5 and 3

Posted in Joomla
Check for joomla's default page

In Joomla 1.5 this was quite easy as there was generally only one homepage (multiple in Joomfish but the same check worked for all). With the introduction of multi-language in 1.6 (or was it 1.7?) it was made a bit more difficult as it is possible to have multiple homepages, and checking for them involves using a user entered string which could be set to anything.

Checking for the default page uses the language tag which is entered by the user. On a single language site this isn't needed, you can just get the menu object and check if it is the default like this.

$menu = JFactory::getApplication()->getMenu();
if ($menu->getActive() == $menu->getDefault()) {
# code...
}

If the site is multi-language you need to check if the menu is default for the current language like this

$menu->getDefault( 'en-GB' )

That might be fine if you have set the site up and know what the language tags are, but if someone decides to add a new language or change the tag of the current one it will stop working. You could loop through all the installed language tags but it is faster to just get the currently active one and check if the current page is the default one for that language like this.

$menu = JFactory::getApplication()->getMenu();
$lang = JFactory::getLanguage();
if ($menu->getActive() == $menu->getDefault( $lang->getTag() )) {
# code...
}

You can read more about Joomla's language here