Query variables and URL rewriting magic
-
So, I’ve searched and searched and I’m most of the way to where I want to be with this code…
What I’m trying to do is create a site that is targeting one of four different demographic types — students, parents, teachers and counsellors. Each one has a different presentation of the page(s) they are looking at — mostly different sidebars and possibly some different text on the page.
What I’ve got is a query variable that I’m adding to the url for each, so there’s a link on the home page for students to go to the /my-page/?dem=student version of that page and parents to go to the /my-page/?dem=parent version of the page. That’s all working.
Now I want to make /my-page/?dem=student turn into /student/my-page/. Which I’ve done using
query_vars
andrewrite_rules_array
filters. Bingo. Now I can go to /student/my-page/ and I get my custom content that’s just for students.What’s the problem?
So, the idea with this site is it’s a bunch of colleges and they’re presenting different information for the different groups and there’s like 30 or so different campuses. So instead of /my-page/ it’s really going to be like /campus23/ (or, to be more accurate /cityname/). Each campus is a custom post type post.
My problem is that everywhere I look at tutorials and examples of
rewrite_rules_array
filters, it’s usually something like this:add_filter( 'rewrite_rules_array', 'my_new_rewrite_rules' ); function my_new_rewrite_rules( $rules ) { $new_rules = array( 'students/([^/]+)/?$' => 'index.php?pagename=my-page&dem=students', 'parents/([^/]+)/?$' => 'index.php?pagename=my-page&dem=parents', 'counselors/([^/]+)/?$' => 'index.php?pagename=my-page&dem=counsellors', 'teachers/([^/]+)/?$' => 'index.php?pagename=my-page&dem=teachers', ); $rules = $new_rules + $rules; return $rules; }
…and my problem is that I don’t want it to be just
pagename=my-page
, I want the rewrite rules to apply to any post or page that has the ?dem={querystring} variable attached to it. Because otherwise, that would mean creating a list of rewrite rules that applied to every single campus as well as any other pages that may be available to those groups.There has to be a way to do this, right?
- The topic ‘Query variables and URL rewriting magic’ is closed to new replies.