Url Rewrite, Multiple Parameters/Templates – Dynamic Path/Parameter Placement
-
We’re having some trouble figuring out WP URL rewrite rules when we have tokens separated by parameters. Very briefly, we’d like to have the following common format for user pages and video libraries:
/users/steve/videos/2
…where “users” is the PHP page template that displays details on a user. So if we go to:/users/steve/
…we see Steve’s home page. If we go to:/users/steve/videos/
…we hit the page template for the video library, and we see Steve’s list of videos. And if we go to:/users/steve/videos/2
…we hit a third page template, the video player page, where we see video id 2 displayed.So we have three separate page templates, and we would like the child / parent relationship between them to be user page -> video library page -> video player page, for our breadcrumbs display.
The trick we can’t figure out is that “steve” and “2” are really params inserted into the url, interrupting the parent/child token relationship. And second, for the video player, there really isn’t a token there at all that maps to the video player page template. It’s just the presence of the “videos” token plus the additional numerical parameter that tells us this maps to the video player page template.
As alternative, we’ve figured out how to get /users/videos/steve/2 working. We’ve added this rewrite rule to our child theme’s functions.php file:
add_rewrite_rule( '^users/videos/([^/]*)/([^/]*)/?', 'index.php?pagename=users/videos&users_filter_name=$matches[1]&user_video_id=$matches[2]', 'top');
(we’ve also defined the variables “users_filter_name” and “user_video_id” and we are successfully parsing them in the template)
But since what we really would like is /users/steve/videos/2 we tried updating the rule to:
add_rewrite_rule( '^users/([^/]*)/videos/([^/]*)/?', 'index.php?pagename=users/videos&users_filter_name=$matches[1]&user_video_id=$matches[2]', 'top');
That didn’t seem to work (it just loaded the ‘/users/’ page) so we’re obviously doing it incorrectly. Is what we want achievable with this structure?
Thanks!
- The topic ‘Url Rewrite, Multiple Parameters/Templates – Dynamic Path/Parameter Placement’ is closed to new replies.