is_front_page() malfunction?

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.

Leave a Comment