I’m trying to get WordPress to recognize a URL structure like this:
example.com/parent/page/{id}/{first_name}/{last_name}
The basic ideea is that I need to access the id
, first_name
and last_name
in my page and they have to come via the URL. So I have to put rules like:
(/some_page/child_page/)/([0-9]+)/([^/]+)/([^/]+) => index.php?pagename=$matches[1]&id=$matches[2]&fname=$matches[3]&lname=$matches[4]
for every page, by using the page_rewrite_rules
filter.
The question is: How to I get the “pagename” for every page? The /some_page/child_page/
part. I can get the post_name
but then I miss the parent, grandfather page.
The pattern WordPress uses to recognize pages is
(.+?)
, which will match anything, but is not greedy, so it allows you to put something at the end. The following code works for me in WordPress 3.0.1: it places the extra pattern at the second-to-last place of the list, because the final pattern is so generic it will match every URL:Be aware that pages, like any post, can also be split into multiple pages with the
<!--nextpage-->
snippet, and the normal URL for that is/pagename/2/
, which might be confusing when you also have/pagename/3/jan/fabry/
where the3
is an ID. You can change the pattern to include an optional page number at the end, so this can become/pagename/3/jan/fabry/2/
where the final2
is the page number, and the first3
is the ID.If you are changing the rewrite rules I recommend my rewrite analyzer plugin (soon in the repository, but get the current version via Dropbox), it helps you debug these things.