4

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?';
}
5

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

  • 1
    I think that's the second time today that we've posted exactly the same answer, at exactly the same time. So, as with the other one, an upvote for you. :) – Chip Bennett Feb 9 '12 at 19:47
  • Ha, I didn't notice the other one! back at you! – chrisguitarguy Feb 9 '12 at 19:48
  • see my comment above. Adding the function to the init action does not work either. I also tried 'plugins_loaded" as Chip suggested but that does not work either. – Mike Feb 9 '12 at 19:56
  • It would be helpful to know what exactly you're trying to accomplish. You can always hook in later: template_redirect for example. – chrisguitarguy Feb 9 '12 at 19:59
  • I need to remove_action() if the user is viewing the front page. The best place to do this is in functions.php, but that seems easier said than done. – Mike Feb 9 '12 at 20:02
5

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() ;).

4

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.

  • plugins_loaded and setup_theme do not work for me. – Mike Feb 9 '12 at 19:57
  • You need to clarify exactly what you want to do. Your example code in your question edit isn't really helpful. For one, you can't echo anything at init; for another, clearly, that's not what you're really after. – Chip Bennett Feb 9 '12 at 20:11
  • See edited answer. – Chip Bennett Feb 9 '12 at 20:15
1

The earlier hooks that template_tags are working is wp action hook. All hooks before that one like init hook won't work for template tags like is_front_page()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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