Steve Taylor photo

WordPress theme files processing order

I was just writing something in a new WordPress theme that required me to output the title of an archive page twice (once for the main title tag, once in some breadcrumbs). I’ve got a series of if / else statements in archive.php that construct the title, depending on if it’s a category archive, a date archive, an author archive, etc.

I thought, rather than duplicating this code, I would move it into my initialization code in functions.php, where I define some constants. I used to stick this stuff at the top of header.php, but I thought I’d try and keep that cleaner and begin directly with the DOCTYPE. Anyway, this arrangement has hit a snag!

Basically, to set these constants I was making use of WP conditional tags like is_archive(). And they just weren’t coming through.

I’ve not 100% confirmed it yet, but thanks to WP Roadmap, I can say that my strong suspicion is that when functions.php is included, the main posts query in the WP hasn’t been run yet. I guess this is so anything added to hooks for that process aren’t missed out? Anyway, the upshot is that tags like is_archive() won’t work in functions.php if they’re called directly (i.e. not part of actual functions).

In a way, duh! It’s called functions.php for a reason I guess. I’ve just got used to it as a handy place to include any initialization code and hacks that get run as soon as the file’s included.

Stepping back and looking at things now, I think the smart thing to do is use the get_header hook. So, for initialization code in functions.php:

function slt_init() {
	define( 'SLT_IS_BLOG', ( is_home() || is_single() || is_archive() ) );
	[etc...]
}
add_action( 'get_header', 'slt_init' );

Anyone else have good tips for theme initialization code?

UPDATE: I’ve just stumbled upon this Query Overview article on the Codex, which is a great little plain English run-through of WordPress’ basic request processing structure. Recommended!

3 comments

  1. JeffM avatar JeffM

    Steve:

    Have you considered a static variable in a getter function in functions.php?

    First time you call the getter from within the title tag, the getter gets the title, storing it one time as a static.

    Next call to the getter (from wherever) returns the same. Avoids gunking up the global space.

  2. Steve Taylor avatar Steve Taylor

    Thanks Jeff – having absorbed all my programming knowledge via ColdFusion rather than a “proper” language, there’s a lot of basics I only slowly pick up on. Without knowing what I’m after I’ve nearly got to “static” variables a few times, but never nailed it.

    I’m pretty happy with constants at the moment, but I’ll bear the getter / static technique in mind for when it works better.

  3. JeffM avatar JeffM

    Steve:

    OK, I see where you’re coming from.

    It’s a given that if you rely on archive.php as your catch-all template, you’re bound to write a lot of conditional logic.

    How’s about some more specific templates, so WP will have made at least one decision for you?

    Personally, I try to limit my use of hooks to situations where they’re the only way to do the job in hand. There is an overhead, after all.

    And by using a globally-accessible function rather than a constant, I’ve abstracted somewhat, and I have future options that don’t involve trawling all the code to modify references to constants.

    Just a suggestion ;)

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>