Detect the WordPress homepage

User avatar
axew3
w3all User
w3all User
Posts: 2677
Joined: Fri Jan 22, 2016 5:15 pm
Location: Italy
Contact:

Detect the WordPress homepage

Post by axew3 »

To detect always, using any permalink, into any stack point, if we are into WordPress homepage, this is the way
( also answered here, because others are all wrongs and there is no answer/solution over all internet (almost for what i have find out): https://stackoverflow.com/questions/492 ... 3#71355373 )

Code: Select all

if(!empty($_SERVER['REQUEST_URI']))
{
 if(substr($_SERVER['REQUEST_URI'], -1, 1) == '/'){
  $REQUEST_URI = substr($_SERVER['REQUEST_URI'], 0, -1);
 }

 $siteUrl = get_option('siteurl');

 if(substr($siteUrl, -1, 1) == '/'){
  $siteUrl = substr($siteUrl, 0, -1);
 }

if(!empty($REQUEST_URI)){
  $r = strpos($siteUrl, $REQUEST_URI);
  if($r !== false)
  { $HOMEPAGE = true; }
 } elseif ( $_SERVER['REQUEST_URI'] == '/'  OR empty($REQUEST_URI) ){ $HOMEPAGE = true; }
}

var_dump($HOMEPAGE);