When is code in functions.php executed? Is it during the loop? The following does not work when placed in functions.php (not within a function) :

if (is_page( 27 )) {

echo 'this is the apply page';


The codex specifies that is_page() cannot run within the loop.

2 Answers 2


is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.

Wait for a proper action.


add_filter( 'template_include', function( $template ) {
    if ( is_page( 27 ) )
        echo 'this is the apply page';

    return $template;

You can call is_page() in a loop too.

  • Thanks. Did you mean 'template_redirect' instead of 'template_include' as the first argument to add_action? If you can call is_page() in the loop why does the codex say you can't? Just out of date? If so, is there a better reference?
    – codecowboy
    Commented Dec 12, 2014 at 12:23
  • @codecowboy You can use both hooks or any other hook later. The codex is full of questionable information. The only reliable source is the WordPress code.
    – fuxia
    Commented Dec 12, 2014 at 12:28

First you need to call page ID via get_the_ID(); function, then you can use is_page function like this:

    $id = get_the_ID();

    if ( is_page( $id == 2591 ) )
    return __( 'Add to cart', 'woocommerce' );
    return __( 'BUY NOW', 'woocommerce' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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