0

I have what I think should be a really simple problem. But for some reason I can't crack it.

I want everything following an existing page to be directed as a query arg to that page. So

example.com/mypage/this/that/other

example.com/mypage/this/that

example.com/mypage/this

should all lead to

example.com/mypage with query cpath=/this/that/other (or whatever followed)

Any thoughts? Using standard references hasn't lead to anything but 404 pages

Thanks,

1 Answer 1

1

You could add a rewrite rule. Rewrite rules are the regular expressions that WP uses to parse out the query from friendly URLs. But I think this would potentially mess things up. How would WP distinguish between /this-is-a-page/this-is-a-child-page/ and /this-is-a-page/this-is-a-parameter/?

A better option, I think, would be to hook into the parse_request action. This gets called after WP loads what it thinks are the query vars, but before it makes the query (and so before it decides that the request should be "not found.") This action hook passes the WP object to your function by reference.

add_action("parse_request", "my_parse_request");

/**
 * @param WP $wp
 */
function my_parse_request(&$wp){
    echo "<pre>";
    var_dump($wp->query_vars);
    die();
}

For an existing page server.loc/sample-page/ this outputs:

array(2) {
["page"]=>
string(0) ""
["pagename"]=>
string(11) "sample-page"
}

Adding parameters... server.loc/sample-page/parameter-1/parameter-2/ ...

array(2) {
["page"]=>
string(0) ""
["pagename"]=>
string(35) "sample-page/parameter-1/parameter-2"
}

Since $wp is passed by reference you can change $wp->query_vars directly, after checking what parts of page_name are actually pages and storing your parameters for use later.

Note that page in $wp->query_vars refers to the page number, if any. All you should touch is pagename.

The code from which parse_request is called is in wp-includes/class-wp.php.

1
  • Thanks so much for the indepth response. This is great. I'll start hacking away at that mechanism. THanks!
    – seth
    Commented Aug 12, 2011 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.