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

Mike Schinkel mikeschinkel at newclarity.net
Sat Nov 28 12:58:05 UTC 2009


On Nov 27, 2009, at 2:31 PM, Otto wrote:
> On Thu, Nov 26, 2009 at 1:58 PM, Mike Schinkel
> <mikeschinkel at newclarity.net> wrote:
>> Are you open to considering that, per chance, you understand how Wordpress works internally better than perhaps 99.99% of other users, and that for the vast majority of people the way it works is extremely obtuse?
> 
> Certainly. However, I'm more open to perhaps a suggestion of better
> documentation or specific improvements. Jacob and you are suggesting
> more extreme changes,

No, Jacob and I have issues but Jacob and I are suggesting different thing. IMO, mine are not extreme changes.

> and doing it in a way that make me believe that
> you doesn't understand the layout of the code, which is why I'm asking
> questions. That's why you ask questions: to try to understand things
> better.

That could well be.  But telling me that I don't understand the layout of the code is in conflict with your prior assertion that it is very easy to understand, don't you think?

> 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? ) 

So I'm not sure it is fully robust and inline with all of Wordpress; there many be many use cases with which the code is incompatible because I didn't set something right in $wp_query but it's very hard to know for sure without testing all those use cases. And that's one of the keys to the problem. There are so many things that can potentially break but that you can't know until you test the use case.  That's why such functionality needs to be in core, because only in core will all the use cases ever get fully fleshed out. 

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.

> However, all I'm getting back is a bunch of nonsense and confusion.
> There seems to be a large amount of misunderstanding about what the
> various parts do, and I'm trying to figure out why.

The misunderstanding about the various parts is simply because the current setup is extremely confusing in spite of your assertion to the latter.

Now I'm not going to suggest if be moved to an MVC; that would be too much for the community to bear.  Instead, I'm simply asking that arbitrary URL routing and post types be added to core as a first class concept; integrated into the core and tested. 

I'm not even asking that the core be changed over to use arbitrary URL routing for everything; I'm just asking that the core make some intelligent decisions based upon incoming URLs that have been mapped to something other than the standard patterns in for Wordpress.  If an routed URL is not matched, nothing would be different.

>> I'm not specifically supporting Jacob's suggestions but currently there are huge difficulties in getting Wordpress to respect the use of URLs that are not designed to support the hardcoded assumptions baked into Wordpress core. What's more, my use cases are not obscure, i.e. "products/{product}", ability to have a list of Posts be children of a Page, and things like that.
> 
> The cases you given to me all seemed extremely simple to do, so I'm
> having difficulty understanding what's so hard about them.

They appear to be simple, until you start trying to implement them.  If they had been simple I would be on my merry way. But my plugin, which I sent you, required lots of fiddling to get working and then I'm not even sure it is robust.

Sure you can get the URLs to route but the state of Wordpress for those URLs is often the issue. Each URL type wants a different state loaded and Wordpress gets in the way, forcing it's idea of what should be done on you and doesn't give you the choice of what to load.  It *assumes* you are using one of its standard patterns or you are using nothing and that you have to handcode much of the code run during standard routing that is already in core but not run the way you need it.

> Me and others have given cope snippets trying to illustrate our points. For
> example, adding /product/whatever should be a simple case of using the
> add_rewrite_tag function. I'm trying to get you, or anybody, to
> explain why you think that's so difficult to do.

Let's use an analogy.  I ask you how to get to town.  There are two roads; one that is a mile and one that takes me 50 miles over the mountain and back.  I say I need a 3rd road and you scoff that the 1 mile road is all you need.  You of course ignore that the 1 mile route includes a 100 foot crevice over which there is no bridge.

So yes, you code snippets route the URL. But Wordpress core does not respect that the URL was routed and thus doesn't let us set up state as we need it.  Therein lies the rub.

Here's a question: Have you actually ever implemented a Wordpress site that actually uses custom URLs in the manners we are attempting. My guess is you haven't otherwise the roadblocks left in the core would be immediately apparent.

Try:

1.) Routing URLs to a "custom post type" (the type can be defined in a custom field for this use case, although having it defined in wp_posts table would be the ideal.)  Load the desired post without duplicating lots of code in core and without having to write tens of lines of code to set $wp_query that 99.9% of developers would not know how to do.

2.) Routing URLs to a custom list of posts (the custom list can be defined by a category or by a tag.) Load the desired list of posts without having to write tens of lines of code that to set $wp_query that 99.9% of developers would not know how to do.

>> Back on the 20th I email you directly with code
> 
> Thanksgiving. I've been out of town for the last week and unable to
> examine your code. I'll get to it soon.

Sorry. I assumed since you were replying on the list  in opposition to the concern you had either dismissed or ignored what I sent.

>> My point is that you keep saying how great the current system of URL routing but ignore examples that illustrate that it is not.  A little acknowledgement of the real issues would be nice.
> 
> A little explanation of those issues in a way that is totally
> non-confusing would be superb.

Code is poetry. I sent you code.  Please look at it as it contains code that illustrates the complexity. 

> Rewriting has nothing to do with the query which has
> nothing to do with the templates... 

Yet discussing one without considering the ramifications on the other two is I believe why you are having problems understanding. The three are not independent in use; they are a holistic three and have to be considered together.

> Can you explain the problems you have in simpler terms, perhaps? I do
> want to help, but *I really don't understand your issues*.

Simplest terms I know.  URL routing is not too hard, but harder than it should be.  Getting Wordpress to load what's appropriate based on the URL routing requires major surgery. Once you start routing URLs there are content patterns that Wordpress core did not previously envision and getting those patterns to work reliably, robustly and without running excess expensive code requires far too much effort.

-Mike Schinkel


More information about the wp-hackers mailing list