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

Otto otto at ottodestruct.com
Sat Nov 28 19:13:00 UTC 2009


On Sat, Nov 28, 2009 at 6:58 AM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> 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?

Yes, but then again no. Basically, you're wanting to combine three
systems into one, which makes the whole of the system more complex and
confusing.

I'm just guessing at what your code does, but to me it appears to:
a) Create a new rewrite rule for products/whatever.
b) Push those off into a new template called custom/product.php
c) Get some kind of special Page data from the database to display there.

IMO, it's too much. You're overthinking it by trying to multiplex
everything together. Each piece should be handled separately, and in
some kind of en masse way.

> (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.)

Custom post type support is improved in 2.9, although I have not
examined the new code in detail.

> 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;
> }

A lot of this seems to me to be required only because you're
attempting to use the existing $wp_query instead of overriding it with
your own in some fashion. Have you considered doing something along
these lines instead?

global $wp_query;
$wp_query = new WP_Query(array('whatever'=>'XYZ' ...));

Or even better, just do this:
query_posts(array('whatever'=>'XYZ' ...));

I mean, the existing wp_query is basically irrelevant to you, yes?
You're wanting to get your own page based on your own parameters. So
just do that and dump the pre-existing query.

Perhaps that is overly simplistic and I need to examine your code
further. I'll try to get to that on Monday.

> (what's "is_single" and how does it compare to "is_singular?")

Single posts only (is_single) vs. any page that shows only a single
post (is_page or is_single or is_attachment).

> 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.

The URL rewriting is already quite capable of that, but directly
linking that into some kind of template routing system seems to be
conflating two entirely separate ideas to me. It's unnecessary,
basically. All you really need to do is to add a rewrite to pull your
variable in from the URL, then use a template_redirect to detect that
variable and call any template you like accordingly. Sure, you could
make a function to do that all in one stroke, but why? It adds
unnecessary complexity to the code, and leaves people thinking that
rewrites == custom templates, when nothing could be further from the
truth.

I took a quick glance at the code (not reading it thoroughly yet) and
it seems to me that the majority of the complexity lies in your
modification of the wp_query. Getting the data you need from the posts
table, basically. The rewriting and template redirection is
straightforward, albeit you seem to have done it the (very) hard way.
I'll try to work up a simpler example to show you what I mean.

> 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.

Agreed, WP does indeed want you to do it its own way, however that
doesn't necessarily mean overriding the existing system with changes.
Bit of WordPress are defined as classes for a reason. Just instantiate
new ones with your own parameters instead, then stick them in the
proper globals.

> 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.

Implemented for a production system, no. Rewrote URLs for fun and
profit, yes. :)

> 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.

I really disagree with this, in a big way. Not every rewrite is going
to use a custom template or load a custom post. Quite the contrary, in
fact, most rewrites that people want simply change the nature of the
URL, not the nature of what is loading.

However, I do agree that custom post types suck. Hopefully 2.9
improves upon this.

> 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.

Okay. I'm absolutely certain I can create a demo that showcases a
better way to do these than you're describing. I'll work on it soon.

But for now, I'm off to get a beer. Happy Holidays!

-Otto
Sent from Memphis, TN, United States


More information about the wp-hackers mailing list