Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?';
}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

That maybe normal behavior, depending on how exactly you're using is_front_page in your functions file.

If you just dropped it inside the file, it's not going to 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

share|improve this answer
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

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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