[wp-hackers] template_redirect vs. single_template vs. other? (was $wp_rewrite->add_permstruct() vs. add_rewrite_rule())
Mike Schinkel
mikeschinkel at newclarity.net
Tue Dec 8 19:26:39 UTC 2009
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.
-Mike
<?php
/**
* Loads the correct template based on the visitor's url
* @package WordPress
*/
if ( defined('WP_USE_THEMES') && constant('WP_USE_THEMES') ) {
do_action('template_redirect');
if ( is_robots() ) {
do_action('do_robots');
return;
} else if ( is_feed() ) {
do_feed();
return;
} else if ( is_trackback() ) {
include(ABSPATH . 'wp-trackback.php');
return;
} else if ( is_404() && $template = get_404_template() ) {
include($template);
return;
} else if ( is_search() && $template = get_search_template() ) {
include($template);
return;
} else if ( is_tax() && $template = get_taxonomy_template()) {
include($template);
return;
} else if ( is_home() && $template = get_home_template() ) {
include($template);
return;
} else if ( is_attachment() && $template = get_attachment_template() ) {
remove_filter('the_content', 'prepend_attachment');
include($template);
return;
} else if ( is_single() && $template = get_single_template() ) {
include($template);
return;
} else if ( is_page() && $template = get_page_template() ) {
include($template);
return;
} else if ( is_category() && $template = get_category_template()) {
include($template);
return;
} else if ( is_tag() && $template = get_tag_template()) {
include($template);
return;
} else if ( is_author() && $template = get_author_template() ) {
include($template);
return;
} else if ( is_date() && $template = get_date_template() ) {
include($template);
return;
} else if ( is_archive() && $template = get_archive_template() ) {
include($template);
return;
} else if ( is_comments_popup() && $template = get_comments_popup_template() ) {
include($template);
return;
} else if ( is_paged() && $template = get_paged_template() ) {
include($template);
return;
} else if ( file_exists(TEMPLATEPATH . "/index.php") ) {
include(TEMPLATEPATH . "/index.php");
return;
}
} else {
// Process feeds and trackbacks even if not using themes.
if ( is_robots() ) {
do_action('do_robots');
return;
} else if ( is_feed() ) {
do_feed();
return;
} else if ( is_trackback() ) {
include(ABSPATH . 'wp-trackback.php');
return;
}
}
?>
On Dec 8, 2009, at 11:10 AM, Otto wrote:
> A "permastruct" is a permalink structure as defined by some group of
> rewrite tags. So /bob/%bob% would be a permastruct, if "bob" was a
> valid rewrite tag.
>
> What's a rewrite tag? That's a variable tag surrounded by % marks.
> Just like you can use in the Permalink settings. Like %month% and
> %day% and %postname% and so forth. A rewrite tag, once it's parsed out
> of the URL, gets turned into some variable in the main $wp_query
> object.
>
> Adding a permastruct does (eventually) add a rewrite rule, internally.
> If I were to add the permastruct of /wacky/%post_id% , then a URL of
> /wacky/4 would bring up the single post with ID #4.
>
> You can add your own rewrite tags with add_rewrite_tag, but custom
> tags don't generally do anything unless you add something else to read
> and use their values.
>
> Using add_rewrite_rule directly, on the other hand, is a bit more
> manual. It just defines a conversion method between a URL and another
> URL. That "other" URL is very arbitrary, you can define variables
> however you wish.
>
> That help?
>
> -Otto
>
>
>
> On Mon, Dec 7, 2009 at 11:50 PM, Mike Schinkel
> <mikeschinkel at newclarity.net> wrote:
>> Hi all:
>>
>> Okay you WP_Rewrite() gurus, now's your time to shine.
>>
>> So I've been trying to figure out $wp_rewrite->add_permstruct() vs. add_rewrite_rule() for the past several days. I've been able to get the former to work but not the latter. I'm trying to figure out when we would use one and when we would use the other?
>>
>> It *seems* like using add_permastruct() can get you support or things like custom single_template and that add_rewrite_rule() would require use of template_redirect, but I can't be 100% sure?
>>
>> Can anyone shed some light on these two? Google has not been my friend in the case of these two functions and how to best use them.
>>
>> -Mike
>>
>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list