[wp-hackers] template_redirect vs. single_template vs. other? (was $wp_rewrite->add_permstruct() vs. add_rewrite_rule())

Otto otto at ottodestruct.com
Tue Dec 8 19:54:26 UTC 2009


Well, it depends on what exactly you want to do. Hard to say without
knowing that, precisely.

You'd use template_loader when you want to bypass the templates system
altogether. Basically, templates are all the files in the theme
hierarchy. Single.php, page,php, category-x.php, archive.php, etc. All
these templates can be overriden directly, of course, so
template_loader is for when you want to go completely outside the
norm.

For the *_templates, the actual filter is performed on
{$type}_template, where type is the argument to get_query_template. So
you can override any of them. 404, author, archive, date, paged,
search, single, attachment, and any mime type attachments (text/plain
would be "text" and "plain" and "text_plain", image/gif would be
"image", then "gif", then "image_gif", etc...). So it's pretty wide
open there.

Which branch of the hierarchy is examined depends on the contents of
the $wp_query. If is_single gets set (because you got a single post),
then it goes to the single branch first, etc.


In essence, you need to determine three things.
1. What do the URLs look like?
2. What posts should be pulled?
3. What template file should be loaded?

Each of these will be handled separately, however some of them can be
skipped or made simpler if there's already support in WordPress for
that specific case. In the case of "pulling one and only one post",
then wp_query can already do that (by setting the %post_id%), and the
single branch of the template hierarchy handles that just fine. In the
case of pulling, say, custom post types, then it's a tad more
complicated.

-Otto



On Tue, Dec 8, 2009 at 1:26 PM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> Thanks Otto, yes, it helps a lot, thank you.
>
> Now that I understand permastructs and rewrite rules better I think the follow on question regarding URL rewriting is how do you get WordPress to do what you want after  you've mapped the URL structure?  Note I changed the subject so that this thread my get indexed by Google for those keywords.
>
> I'm at the point where I'm almost understanding that an arbitrary URL that has no need for any core support in WordPress would use template_redirect whereas one would use other hooks based on what is needed.  I've learned, thanks to Dion (dd32) that rather than hook all those nasty post_* hooks I could use the single_template hook when what I wanted to do was in essence load a post; that was so far from obvious (for me) that I never even looked there and Dion's example was the first I'd seen (the only one on the web?)
>
> So my next question is, what hooks are available in(directly in) template-loader.php and when should we use each one of them? (I've copied the file into this email below for reference.)  For example, with single_template it appears we'd hook that when we want to have a single post loaded and ready for display; that one is more obvious now, but what about all the rest?  I think it would be a huge service for future explorers if we could collectively document all the ways you might hook templates for custom URLs and in what context you would use each hook for the benefit of future explorers and then, as Matt requests, move to the Codex.


More information about the wp-hackers mailing list