Check if current page is frontpage?

Check if current page is frontpage?

by Zarah Khan -
Number of replies: 10

Is there a way to check if the current page is the site's home page/frontpage?

I am looking for a generic way, that is, right now I am working on localhost, so my frontpage is at http://localhost/moodle/, but for a production site, it could be anything, like http://localhost/abc.com

Thank you in advance.

Average of ratings: -
In reply to Zarah Khan

Re: Check if current page is frontpage?

by Darko Miletić -

This should cover you

global $PAGE;

if ($PAGE->has_set_url()) {
if ($PAGE->url->compare(context_system::instance()->get_url(), URL_MATCH_BASE)) {
echo 'homepage';
}
}


Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Check if current page is frontpage?

by Darko Miletić -

The above code will work only when page object has configured url. When that does not happen the code will not serve. This function covers that case as well:

/**
* @return bool
* @throws Exception
* @throws dml_exception
*/
function is_homepage() {
global $PAGE, $ME;

$result = false;

$url = null;
if ($PAGE->has_set_url()) {
$url = $PAGE->url;
} else if ($ME !== null) {
$url = new moodle_url(str_ireplace('/index.php', '/', $ME));
}

if ($url !== null) {
$result = $url->compare(context_system::instance()->get_url(), URL_MATCH_BASE);
}

return $result;
}


It is a blatant example of defensive programming but with Moodle you can never be too sure.


Average of ratings: Useful (2)
In reply to Zarah Khan

Re: Check if current page is frontpage?

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Is there any reason why something like the simple if statement below wouldn't work or is bad practice?

global $PAGE;
if ($PAGE->bodyid == 'page-site-index') {
    your code here
}

Provided you know the bodyid of the kind of page you are looking for, you can use it for many things.

In reply to Richard Oelmann

Re: Check if current page is frontpage?

by Darko Miletić -
that one is also valid. There is this one as well:

if ($PAGE->pagetype == 'site-index') {
// frontpage
}


As usual there is at least 5 ways to do something.
Average of ratings: Useful (2)
In reply to Darko Miletić

Re: Check if current page is frontpage?

by Darko Miletić -

The reason why I presented URL approach is that sometimes pagetype or bodyid is not as clear as we might think, outside of the frontpage, so if someone stumbles upon an issue url approach is an alternative.

In reply to Darko Miletić

Re: Check if current page is frontpage?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Also:
if ($PAGE->pagelayout == frontpage') {
In reply to Gareth J Barnard

Re: Check if current page is frontpage?

by Darko Miletić -

Unfortunately pagelayout is not fully conclusive. Theoretically anybody can write a page and set it's layout to frontpage. I would not rely on this.

$PAGE->set_url('/somedummynonfrontpage.php');
$PAGE->set_pagelayout('frontpage');



In reply to Darko Miletić

Re: Check if current page is frontpage?

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

True, but why would they?  It's more likely to be a mistake if they do.  In any event the code will render the frontpage layout file, which is what might be needed anyway.

People could also manipulate the code to adjust all of the other solutions too.

In reply to Darko Miletić

Re: Check if current page is frontpage?

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

'sometimes pagetype or bodyid is not as clear as we might think'

Is that an issue that needs addressing then Darko? So that they are clear.

Can you give any examples where they are not?


I needed this solution for my own theme recently - but I'll stick with the KISS approach I think and deal with more complex issues IF they ever happen smile

In reply to Richard Oelmann

Re: Check if current page is frontpage?

by Darko Miletić -

For example, as you may or may not know the standard page type for course frontpage is course-view-[configured course format name], unfortunately [moodle]/course/view.php is not the only page with that type.

In core Moodle there are 2 places where this type is configured:

[moodle]/user/index.php (click to see the code) and [moodle]/user/view.php (click to see code)

So if I write some navigation extension which relies on fact that /course/view.php will be equal to pagetype 'course-view-[format]' I will be sadly bitten by the truth. Moodle is loaded with these inconsistencies and when you develop something you can seldom wait for something to be fixed in core. The only certain thing that has 1-1 relationship with a page is page url.