Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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,

share|improve this question
add comment

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

share|improve this answer
    
Thanks so much for the indepth response. This is great. I'll start hacking away at that mechanism. THanks! –  seth Aug 12 '11 at 15:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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