[wp-hackers] WP_Rewrite query

Andy Skelton skeltoac at gmail.com
Sat Feb 4 07:00:36 GMT 2006


On 2/3/06, Rob <powzor at gmail.com> wrote:
> but I discovered that the rewrite will only work if you're rewriting to
> index.php

Right. Wp_rewrite is not a replacement for mod_rewrite. It only
rewrites internally. To rewrite to other scripts without mod_rewrite
would require browser redirection. By keeping its scope limited to
index.php, we never have to redirect the browser.

> ...displaying the first post in my blog when you visit /library/. I
> thought then that I could just redirect to "index.php?library=true", and
> then hook into init() to check $_GET['library'] and do what I want if it
> was set, but no dice; it never seems to pass the variable in the query
> string, as var_dump()ing $_GET gives an empty array every time.

First, the init action is earlier than wp(), the function that
eventually calls $wp->parse_request(), the function that checks the
request against the wp_rewrite rules.

Second, wp_rewrite isn't going to recognize your query var unless you
add it to the public_query_vars array. There's a filter for that in
classes.php line 1584: query_vars.

Third, you're not going to find it in $_GET because WP doesn't
populate that with wp_rewrite query vars. Instead they get put in
$wp->query_vars.

Fourth and last, why bother with wp_rewrite when all you need to do is
check the request URI for your pattern and take over if it matches? I
don't know what your plugin is meant to do but I can tell you this: if
your "library" action doesn't need to modify the posts query, you can
forget about wp_rewrite.

Fifth and not even in the list, I disclaim all responsibility for the
forgoing nonsense and any consequences for putting same into practice,
real or imagined. ;-)

Andy


More information about the wp-hackers mailing list