[wp-hackers] Options for Controller - Views

Otto otto at ottodestruct.com
Fri Nov 20 16:17:50 UTC 2009


On Fri, Nov 20, 2009 at 9:29 AM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> Yes it is a nasty, dirty, ugly hack.  In order to get something as simple as an arbitrary URL to route requires use of the follow hooks (directly from my plugin):
>
>                add_action('template_redirect',array(__CLASS__,'custom_url'));
>                add_action('generate_rewrite_rules', array(__CLASS__,'add_rewrite_rules'));
>                add_filter('query_vars', array(__CLASS__,'add_queryvars' ));
>                register_activation_hook(__FILE__,array(__CLASS__,'flush_rewrite_rules');
>                add_action('pre_get_posts',array(__CLASS__,'set_query'));
>                add_action('parse_query',array(__CLASS__,'parse_query'));
>                add_filter('the_content',array(__CLASS__,'filter_post_list_content'));
>                add_filter('posts_fields',array(__CLASS__,'filter_posts_fields'));
>                add_filter('posts_where_paged',array(__CLASS__,'filter_posts_where_paged'));
>                add_filter('posts_join_paged',array(__CLASS__,'filter_posts_join_paged'));
>                add_filter('posts_orderby',array(__CLASS__,'filter_posts_orderby'));
>                add_filter('posts_groupby',array(__CLASS__,'filter_posts_groupby'));
>
> It is total crap that you have to use so many hooks to get the path "/products/{product}" to work and that your hooks have to much with fragile SQL.

Sweet Jebus, man. Why in the *world* would you need all that?

function add_product_var($public_query_vars) {
    $public_query_vars[] = 'product';
    return $public_query_vars;
}
add_filter('query_vars', 'add_product_var');

function do_product_rewrite() {
    add_rewrite_rule('product/([^/]+)/?$',
"index.php?product={$matches[1]}",'top');
}
add_action('init', 'do_product_rewrite');

Then in your template_redirect hooked function, you can do
get_query_var('product') to get the value of your product variable and
act accordingly.

Unless you're doing something completely absurd, I have no idea why
you need all those hooks. Heck, all the posts_* hooks your using have
absolutely nothing to do with rewriting or routing of the resulting
page. Those modify the WP_Query only.

-Otto


More information about the wp-hackers mailing list