8

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 checking PHP_SELF or REQUEST_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.

6
  • Are you definitely using the latest version of WordPress? And is this a multisite setup? Commented Aug 20, 2015 at 14:20
  • I'm using version 4.2.4 and yes it is a multisite setup. Added to question.
    – Ejaz
    Commented Aug 20, 2015 at 14:38
  • This is... odd. All I can suggest for now is using the following if condition: ( ! isset( $GLOBALS['pagenow'] ) || $GLOBALS['pagenow'] !== 'wp-signup.php' ) && ( is_front_page() || is_home() ) Commented Aug 20, 2015 at 15:56
  • Thanks for the suggestion @TheDeadMedic, that'll do it. So the issue is a WP bug then?
    – Ejaz
    Commented Aug 20, 2015 at 18:32
  • I've actually heard of this happening before on WPMU. Try deactivating your theme and reactivating it. Commented Aug 20, 2015 at 21:14

2 Answers 2

1

Just speculation, but I wonder if you're running into an anonymous function problem. Anonymous functions are allowed in WP, and usually work fine (presuming updated PHP), but, if you search around you'll find reports of suspected bugs or at least of unexpected behavior.

For that matter, I'm not sure that I've ever seen an anonymous function used as an example in the WordPress Codex, and I can't recall ever running across one before in theme and plug-in code. Of course, I've not had my eye out for anon functions, but, still, I think the above function will almost always be written in some version of the more familiar two-part format - ie:

add_filter('body_class', 'ejay_add_home_class');

function ejay_add_home_class($classes) {

    if (is_front_page() || is_home()) {

        $classes[] = 'home-page';
    }

    return $classes;
} 

So, as an experiment, I'd try the above more "conventional" format, and also try it with a designated priority higher or lower than 10. If attaching multiple anonymous functions to the same filter, I'd give them different priorities, or use an array (example here: http://snippets.khromov.se/adding-multiple-actions-and-filters-using-anonymous-functions-in-wordpress/ ), or write each one of them as named two-parters, too.

In truth, I find the slightly lengthier 2-part way easier to read, track, and adjust anyway.

-5

WordPress uses different templates for pages on your site. If you have a page.php template in your theme, that will be used to display your pages. If you have a single.php, that will be used to display your single posts. index.php or home.php would display your home page, or page.php if you have a certain page selected as the home page through the reading options.

So, your conditional statement:

<?php if (is_front_page()){ ?>
<p>Home Page</p>
<?php else { ?>
<p>Not Home Page</p>
<?php } ?>
?>

Would only really be useful in the header.php or the footer.php file- or in page.php if you have a certain page selected in the reading options.

3
  • Thanks for the answer but I'm failing to see how this affects the values returned by aforementioned functions.
    – Ejaz
    Commented Aug 26, 2015 at 11:51
  • 5
    STOP SPAMMING!!!!. Your links are junk back to your own website. Your next posts will be flagged as spam. Thank you Commented Aug 27, 2015 at 17:16
  • This also does not even answer the question at hand Commented Aug 27, 2015 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.