I've a pretty basic problem that I'm surprised WP doesn't have a native solution for (unless I'm overlooking something, hopefully).
I've a WP site with static page
set as front page in reading setting. In a plugin code, I'm trying to determine whether WP is displaying the front page and add a class to $classes
array if so is true. I'm using following code to accomplish it
add_filter('body_class', function($classes){
if(is_front_page() || is_home()){
$classes[] = 'home-page';
}
return $classes;
});
I'm using both is_front_page()
and is_home()
in case the front page setting changes from static page to blog layout in future.
The issue I'm encountering is that this code adds home-page
class to body
even on wp-signup.php
page.
Inspecting the code reveals that is_front_page()
calls WP_Query::is_front_page()
, which essentially returns results of WP_Query::is_page(get_option('page_on_front'))
. So the root of the problem is that wp-signup.php
qualifies as the page (id) returned by get_option('page_on_front')
(which returns ID
of the static page set as front page in settings > reading).
WP_Query::is_page()
uses WP_Query::get_queried_object()
internally to decide whether current page is the page present in method arguments. In wp-signup.php
case, the code that sets the current queried object is as following
/*...other code... */
elseif ( $this->is_singular && ! empty( $this->post ) ) {
$this->queried_object = $this->post;
$this->queried_object_id = (int) $this->post->ID;
}
/*...other code... */
This shows that wordpress, for some reason, queries the front page in order to display wp-signup.php
and raises following questions.
- Why
is_front_page()
is returning wrong results? - AFAIK
wp-signup.php
can never be set as home page using Wordpress administrator setting then why wordpress code isn't bailing out just by checkingPHP_SELF
orREQUEST_URI
? - Why does WP_Query have current home page in
$this->post
at this point?
I've ruled out plugin issue by removing the plugins (and mu-plugins) directory. It still qualifies wp-signup.php
as front page where it doesn't for any other page.
Any help regarding this issue will be greatly appreciated.
Update
I'm using WP verison 4.2.4 and it is a multisite setup.
Thanks.
if
condition:( ! isset( $GLOBALS['pagenow'] ) || $GLOBALS['pagenow'] !== 'wp-signup.php' ) && ( is_front_page() || is_home() )