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

The codex quote by chrisguitarguy is no longer valid!! It has been updated to:

Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). 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.

Thus, the init hook no longer works!!! As stated, the wp hook is the first hook you can use if you want to use conditional query tags. So, to make conditional tags work, here is the minimal boilerplate:

add_action('wp', 'function_that_uses_conditional_tags');

function function_that_uses_conditional_tags() {
    // place code here
}

However, please note that while that "warning" seems universally applicable to conditional tags as it appears in the very introduction to the codex page about Conditional Tags, it fails to emphasize that it really only applies to the frontend.

The wp hook does not even fire in the backend! So if you use it for instance with is_admin(), it will never evaluate to true. Use a different hook in that case. Actually, init works just fine for is_admin() ;).


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


This is expected behavior. The functions.php file is parsed before the query is setup and available, so if you have if ( is_front_page() ) sitting naked inside of functions.php, it will return false, because there's no query yet.

What you need to do is to put your is_front_page() conditional inside of a callback function, that is then hooked into an appropriate action - i.e. an action that fires after the query is setup/available.

For reference, I think functions.php is parsed at plugins_loaded (it might be setup_theme), and the query conditionals should be available at or after init.

Edit

plugins_loaded and setup_theme do not work for me

Of course those hooks won't work for you. They fire before the query is setup. The is_front_page() conditional is only available after the query is setup, which happens at init.

I need to remove_action() if the user is viewing the front page.

You're not really telling us exactly what you're trying to do. Your question simply asked when is_front_page() available, which is what we answered. Knowing how/when to remove an action is a mostly entirely different question. You need to provide the add_action() call you're wanting to remove.