is_front_page only works in theme file, and does not work in functions.php

If I use is_front_page() in my front-page.php file it works as expected. However if I use the same function in my theme functions.php file it does not work. Is this normal behavior? If not can it be trouble shot somehow?

In settings->reading I have my front page set to a page called “front page” so I think that is all set.

EDIT

adding the following to functions.php does not work, however I promise it does work as expected if I add is_home_page() to my header.php file

add_action('init', 'my_test');

function my_test(){
    if(is_front_page())
        echo 'is it?';
}

4 s
4

That may be normal behavior, depending on how exactly you’re using is_front_page in your functions file.

If you use it inside the global scope of functions.php, it will not work.

Why? Because WordPress loads functions.php before the $wp_query object has been set up with the current page. is_front_page is a wrapper around around $wp_query->is_front_page(), and if the query hasn’t been set up, it’s always going to return false (or throw a warning, if you have wp_debug on.

From the codex:

Warning: You can only use conditional query tags after the init action
hook in WordPress. For themes, this means the conditional tag will
never work properly if you are using it in the body of functions.php,
i.e. outside of a function.

http://codex.wordpress.org/Conditional_Tags

Leave a Comment