[wp-hackers] The problem with WP_Rewrite <<< RE: Options for Controller - Views

Dion Hulse (dd32) wordpress at dd32.id.au
Sun Nov 29 06:46:42 UTC 2009


On Sat, 28 Nov 2009 23:58:05 +1100, Mike Schinkel  
<mikeschinkel at newclarity.net> wrote:
>> In other words, I'm trying to explain why I think it's a good system,
>> and trying to get you to explain to me why you think that it is not.
>
> To start, it is very confusing and hard to figure out what you need to  
> do to accomplish a URL routing task in WordPress.  Let's take a look at  
> the following which is what I'd like to be able to do (my plugin  
> actually does it but in a manner I feel is very fragile):
>
> add_custom_url(	array(
>    'name'          => 'product-page',
>    'url_type'      => 'single-post',
>    'url_template'  => 'products/{product_slug}',
>    'template_file' => 'custom/product.php',
>    'post_type'     => 'product',
> ));
>
> To me that seems very straightforward and is a level of simplicity I  
> think we should strive for, don't you think?  (Wouldn't it be great to  
> have custom URLs addable from the admin console? That's what I think we  
> should have. Also note is assumes a custom post type, the other thing  
> Wordpress badly needs to support in core.)
>
> Now what is (some of) the code I have to write to support that?  Let's  
> take a look:
>
> $wp_query->query_vars['page_id'] = 0;
> $wp_query->is_page = false;  // A "single-post" is not same as a  
> WordPress "Page"
> $wp_query->is_single = $wp_query->is_singular = true;
> if ($post_id = self::get_post_id($wp_query)) {
>    $wp_query->queried_object = $wp_query->post = get_post($post_id);
>    $wp_query->query_vars['p'] = $post_id;
> } else {
>    $wp_query->is_404 = true;
> }
>
> Simple?  I think not (what's "is_single" and how does it compare to  
> "is_singular?" Why do I need to set "page_id" to 0? Why do I have to set  
> "query_vars['p']" to $post_id? Why do I have to set "is_page" to false?  
> What else should I have set that I didn't know to set? )
> ..<snip>..
> What's more, there's lots of code that runs in core that, by the time I  
> get enough control in my plugin, I have to unravel and redo. That's not  
> an ideal architecture and that's what I'm looking to have improved.

I think you're hooking too late in the process, and as such, you're  
running into issues related to WordPress looking too deeply into it before  
you tell it otherwise.

Your idea of add_custom_url() is indeed very simplistic, and clean, The  
major problem with that IMO, is its not flexible enough.. Its ok for  
simplistic structures, but i'm certain it'll not last for ever.

For everyones benefit, i've just written a quick plugin which achieves  
what you set out to do with the product rewrite rules, whilst reducing the  
ammount of useless SQL queries performed as well as avoiding some rather  
messy code:

http://dd32.id.au/uploads/2009/11/HTDIR_URL_Rewrite.zip

In a nutshell:
  * 3 filters to control Rewrite, Query, and Template
  * No eronous SQL queries, The only query is the product load query. (I'm  
refering to post queries here btw)
  * Works with WordPress 2.9, 2.8 should work too
  * Fair bit of comments inline, Not finished obviously
  * Takes /products/<slug> and spits out post_type = product
  * Works no matter what your permastructure is ( none, ie.  
?product=product-1, /product/product-1/, /index.php/product/product-1/)
  * Admin panel to add/remove products
  * 2 lines which i would call hackish.. But thats only telling wordpress  
that we're on a custom page.. (is_single = true, is_home = false)
  * Uses a custom page template bundled with the plugin (Taken from  
single.php from default theme, removed a bit of code, bare basics)
  * Uses as much of the API as currently exists for it.
  * Not finished 100%, Final few touches need to be put on, such as  
flushing of rules on deactivation

Yes, This is not a complete writeup, Its just to demonstrate, that the  
code does not have to be as messy as you've quoted, And while its longer  
and more verbose than add_cusotm_url(), its definately a better start.

I'll be writing a series based around things like this, this code is to  
become part of the first post in the series.

I'd like to see register_post_type() extended to take care of custom  
url's, to a certain extent, and i'll be creating a few tickets later for  
3.0 i hope.. 3.0 is going to bring some great changes by the look of it.
I can say however, That register_post_type will NOT be able to do  
everything for you, It will most likely however, manage to take care of  
the basics at first.
-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


More information about the wp-hackers mailing list