[wp-testers] Conditional tags not working

62 views
Skip to first unread message

Nathan Rice

unread,
Jan 17, 2010, 12:21:26 AM1/17/10
to wp-te...@lists.automattic.com
As this post points out:
http://weblogtoolscollection.com/archives/2010/01/16/fix-for-is_home-is_archive-not-working/

conditional tags like is_home(), is_page, is_archive(), etc., are all
seemingly not working. I've noticed this as well (running 2.9.1).

I believe that it might work in some circumstances (like in the loop?), but
I am currently running into the problem when trying to conditionally do
something in the theme's functions.php file.

The article linked to above suggests using wp_reset_query() before using a
conditional, but that does NOT work in functions.php, though it may work in
a file like footer.php.

Does anyone have any clue as to why this is happening? I dumped the
$wp_query object and all the conditionals in them have null values, which is
of course why conditional tags aren't working ... which begs the question,
why isn't the $wp_query object returning accurate values for the
conditionals?

I'm stumped. Any takers?

------------------
Nathan Rice
WordPress and Web Development
www.nathanrice.net | twitter.com/nathanrice
_______________________________________________
wp-testers mailing list
wp-te...@lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-testers

Austin Matzko

unread,
Jan 17, 2010, 12:30:15 AM1/17/10
to wp-te...@lists.automattic.com
On Sat, Jan 16, 2010 at 11:21 PM, Nathan Rice <ncr...@gmail.com> wrote:
> As this post points out:
> http://weblogtoolscollection.com/archives/2010/01/16/fix-for-is_home-is_archive-not-working/
>
> conditional tags like is_home(), is_page, is_archive(), etc., are all
> seemingly not working. I've noticed this as well (running 2.9.1).
>
> I believe that it might work in some circumstances (like in the loop?), but
> I am currently running into the problem when trying to conditionally do
> something in the theme's functions.php file.

Could you be more specific about what you're trying to do? I wouldn't
expect them to work when the theme's functions.php file is included,
for example, because that happens before the page query is even
parsed.

Nathan Rice

unread,
Jan 17, 2010, 12:48:54 AM1/17/10
to wp-te...@lists.automattic.com
OK, something simple and straightforward ...

Let's say I want to create an admin settings page. I have the code loaded in
a separate file, and want to use the functions.php file to include it, but
there's no need to load the code into memory when viewing the front-end
(non-admin). So, I wrap the include in conditional tags ...

<?php
if(!is_admin())
require_once(TEMPLATEPATH.'/admin_menu.php');
?>

That code does NOT work.

Or, let's say I want to move the comment reply script call from the
header.php to the functions.php, where it really belongs ...

<?php
if(is_singular())
wp_enqueue_script( 'comment-reply' );
?>

See? And please don't tell me to write a custom function and use hooks.
That's not the issue here. The issue is the fact that conditionals aren't
working properly. I'm almost positive that this used to work, which is why
I'm wondering why it's not working now.

------------------
Nathan Rice
WordPress and Web Development
www.nathanrice.net | twitter.com/nathanrice

Dion Hulse (dd32)

unread,
Jan 17, 2010, 1:12:06 AM1/17/10
to wp-te...@lists.automattic.com
is_admin() should be working perfectly fine, as core uses it.

You canot use template conditionals in global scope in functions.php
however, This is not possible due to the theme being included earlier than
WordPress has determined what type of query it is.

Try using them at a later stage in the loading process and you'll find
they work, Using a custom function is REQUIRED, and cannot be worked
around if you want to use it within functions.php. An example is:

add_action('template_redirect', '_theme_comment_reply');
function _theme_comment_reply() {
wp_enqueue_script( 'comment-reply' );
}

Alternativly, You can use them at a later stage, Such as within your
header.php file before any output occurs. At this point WordPress has
determined what the request is, and the conditionals will be populated.


--
Dion Hulse / dd32

Contact:
e: con...@dd32.id.au
msn: m...@d32.id.au
skype: theonly_dd32
Web: http://dd32.id.au/

Austin Matzko

unread,
Jan 17, 2010, 1:14:04 AM1/17/10
to wp-te...@lists.automattic.com
On Sat, Jan 16, 2010 at 11:48 PM, Nathan Rice <ncr...@gmail.com> wrote:
> OK, something simple and straightforward ...
>
> Let's say I want to create an admin settings page. I have the code loaded in
> a separate file, and want to use the functions.php file to include it, but
> there's no need to load the code into memory when viewing the front-end
> (non-admin). So, I wrap the include in conditional tags ...
>
> <?php
> if(!is_admin())
> require_once(TEMPLATEPATH.'/admin_menu.php');
> ?>

You do mean "is_admin()", not "! is_admin()", right?

> That code does NOT work.

It should, assuming you remove the "!".

> Or, let's say I want to move the comment reply script call from the
> header.php to the functions.php, where it really belongs ...
>
> <?php
> if(is_singular())
> wp_enqueue_script( 'comment-reply' );
> ?>
>
> See? And please don't tell me to write a custom function and use hooks.
> That's not the issue here. The issue is the fact that conditionals aren't
> working properly. I'm almost positive that this used to work, which is why
> I'm wondering why it's not working now.

Unfortunately, attaching callback functions to the correct hooks is
the issue here. It doesn't make any sense to check is_singular() at
the top of functions.php, because when functions.php is included and
that code executed, WordPress doesn't yet know what the queried page
is--whether it's singular, a page, or whatever.

I would be really surprised if the above example has worked in any
version of WordPress. It's impossible for WordPress to determine
whether the current query is for a singular object, before it has even
parsed the query.

Andrew Nacin

unread,
Jan 17, 2010, 1:16:10 AM1/17/10
to wp-te...@lists.automattic.com
>
> <?php
> if(is_singular())
> wp_enqueue_script( 'comment-reply' );
> ?>
>

Adding to Dion and Austin, wp_enqueue_script() shouldn't be called in global
scope in functions.php and should instead be attached to a hook.

Peter Westwood

unread,
Jan 17, 2010, 6:02:38 AM1/17/10
to wp-te...@lists.automattic.com

On 17 Jan 2010, at 05:21, Nathan Rice wrote:

> As this post points out:
> http://weblogtoolscollection.com/archives/2010/01/16/fix-for-is_home-is_archive-not-working/
>
> conditional tags like is_home(), is_page, is_archive(), etc., are all
> seemingly not working. I've noticed this as well (running 2.9.1).
>
> I believe that it might work in some circumstances (like in the
> loop?), but
> I am currently running into the problem when trying to conditionally
> do
> something in the theme's functions.php file.
>
> The article linked to above suggests using wp_reset_query() before
> using a
> conditional, but that does NOT work in functions.php, though it may
> work in
> a file like footer.php.
>
> Does anyone have any clue as to why this is happening? I dumped the
> $wp_query object and all the conditionals in them have null values,
> which is
> of course why conditional tags aren't working ... which begs the
> question,
> why isn't the $wp_query object returning accurate values for the
> conditionals?
>
> I'm stumped. Any takers?

I have replied on the post as the example there looks very much like a
call to query_posts has been made to run a second query.

As said by others, you cannot use any of the query related
conditionals until the query has been run.

This means that you have to use a hook like template_redirect from a
plugin or functions.php to do conditional specific things.

Basically any hook which runs after WP::query_posts().

Cheers

Peter
--
Peter Westwood
http://blog.ftwr.co.uk | http://westi.wordpress.com
C53C F8FC 8796 8508 88D6 C950 54F4 5DCD A834 01C5

Nathan Rice

unread,
Jan 17, 2010, 3:15:33 PM1/17/10
to wp-te...@lists.automattic.com
My apologies ... I thought I remembered this working at some point before,
but it looks like I was wrong.

is_admin() does indeed work when used in functions.php.

So, as a followup question ... what hooks are best to use if I want to be
able to use the conditionals? Someone already mentioned template_redirect
... are there any others? Is there a good resource that defines what hooks
are fired before and after WP knows what type of query it is?

Thanks!

------------------
Nathan Rice
WordPress and Web Development
www.nathanrice.net | twitter.com/nathanrice


On Sun, Jan 17, 2010 at 6:02 AM, Peter Westwood
<peter.w...@ftwr.co.uk>wrote:

Austin Matzko

unread,
Jan 17, 2010, 3:58:44 PM1/17/10
to wp-te...@lists.automattic.com
On Sun, Jan 17, 2010 at 2:15 PM, Nathan Rice <ncr...@gmail.com> wrote:
> So, as a followup question ... what hooks are best to use if I want to be
> able to use the conditionals? Someone already mentioned template_redirect
> ... are there any others? Is there a good resource that defines what hooks
> are fired before and after WP knows what type of query it is?

What hooks you use depend on what you're trying to do.

I don't know about hook resources, but these three lines in
wp-blog-header.php give a good overall picture of what's happening:

| require_once( dirname(__FILE__) . '/wp-load.php' );
| wp();
| require_once( ABSPATH . WPINC . '/template-loader.php' );

wp-load.php loads all the WordPress functionality.
wp() parses the current query.
template-loader.php decides which template file to include.

Andrew Nacin

unread,
Jan 17, 2010, 4:09:49 PM1/17/10
to wp-te...@lists.automattic.com
On Sun, Jan 17, 2010 at 3:15 PM, Nathan Rice <ncr...@gmail.com> wrote:

> is_admin() does indeed work when used in functions.php.
>

is_admin() works as it isn't like any of the other conditionals. It is
defined early on (in wp-includes/load.php, which is one of the first files
wp-settings.php includes) and relies on a constant set in the admin.php
bootstrap, not the current query like the other conditionals.

So, as a followup question ... what hooks are best to use if I want to be
> able to use the conditionals? Someone already mentioned template_redirect
> ... are there any others? Is there a good resource that defines what hooks
> are fired before and after WP knows what type of query it is?
>

Adding to Austin's suggestion to review wp-blog-header.php, you can add this
to your functions.php file to get a dump of every hook that runs:

add_action( 'all', 'dump_all_hooks' );
function dump_all_hooks() {
var_dump( current_filter() );
}

You could combine that with function_exists or did_action etc. to only see
hooks after something has been loaded or is run.

scribu

unread,
Jan 17, 2010, 8:05:55 PM1/17/10
to wp-te...@lists.automattic.com
A pretty good hook reference is WP Roadmap:

http://wp-roadmap.com/demo/

Basically, any hook that comes after 'template_redirect' will have
conditional tags available. Be careful with query_posts() though.


--
http://scribu.net

Reply all
Reply to author
Forward
0 new messages