[wp-hackers] Make WP_Rewrite eaiser to use

Sam Angove sam at rephrase.net
Sun Feb 5 23:49:40 GMT 2006


On 2/6/06, David House <dmhouse at gmail.com> wrote:
> Reading through the recent thread concerning Scott Merril's troubles
> with WP_Rewrite [1], I thought that we really should automate a lot of
> what is in the final plugin [2].

I think we should arrange some clear code examples. Ryan's is great,
but it doesn't suit every use-case.

Are there any other hooks/filters that get convenience wrappers?
They're pretty convenient as-is. The current way is very flexible, and
it only looks verbose when it's for the sake of one query var -- I had
a plugin with about eight, and three times that many rewrite rules,
and it was very nice indeed.

Scott's plugin is only complicated because there's no good place to
modify other query variables -- changing the query string is hackish,
and parse_query might be too late if the wrong is_* variables have
already been set -- and it modifies the post permalink. The latter, at
least, can't be easily simplified.

> ----------------------------------------------------------------------------
> 1. add_rewrite_rule(): A function for adding a straight rewrite rule.
>
> [snip]
> * Theoretically it could then filter rewrite_rules_array and add the
> rules itself, but I think we should add a function to WP_Rewrite for
> adding 'extra' rules that needn't be generated by
> generate_rewrite_rules().

I don't have a problem with it, but why is filtering the array so bad?
The current way is very easy, and it's more elegant for adding
multiple rules.

function add_arbitrary_rules($rules) {
    $url = get_settings('home');
    $rules[$url.'/arbitrary/(wibble)/?$'] = 'index.php?var=$matches[1]';
    $rules[$url.'/arbitrary/(wobble)/?$'] = 'index.php?var=$matches[1]';
    return $rules;
}
add_filter('rewrite_rules_array', 'add_arbitrary_rules');



> ----------------------------------------------------------------------------
> 2. add_rewrite_tag(): Add a new tag (like %postname%):
>
> What it needs to do:
> * Get a query var name by stripping the % signs from the name of the
> tag: trim($name, '%')
> * Call $wp_rewrite->add_rewrite_tag() with the name, generated QV name
> and regex.
> * Add the QV as a query var (again, this could be done by filtering
> query_vars but it might be nicer to add a function to the WP class
> that stores 'extra' QVs like above)

Without your callback, does this do enough to make it worth the
bother? The query_vars filter is the easy bit anyway, and this API is
much less efficient if you're adding more than one QV.

Not a code reason, but FWIW I like having registration of query vars
separate since it makes the author more likely to remember to use
get_query_var() instead of $_GET. Since in my case that author is me,
aids to memory are very, very welcome... ;)


More information about the wp-hackers mailing list