• Resolved Chris Reynolds

    (@jazzs3quence)


    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 and rewrite_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?

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can use all of the captures in the rules so you can write something like below and cover all of your variations with any page name.

    add_filter( 'rewrite_rules_array', 'my_new_rewrite_rules' );
    function my_new_rewrite_rules( $rules ) {
         $new_rules = array(
                 '(students|parents|counselors|teachers)/([^/]+)/?$' => 'index.php?pagename=$matches[2]&dem=$matches[1]',
         );
         $rules = $new_rules + $rules;
         return $rules;
    }

    Thread Starter Chris Reynolds

    (@jazzs3quence)

    Sweet. I’ll give it a try.

    Thread Starter Chris Reynolds

    (@jazzs3quence)

    The regex at the beginning to match the prefix I’m adding didn’t work, but I was able to use the second half to match the pagename. So this is what I’ve got now which is working:

    add_filter( 'rewrite_rules_array', 'my_new_rewrite_rules' );
         function my_new_rewrite_rules( $rules ) {
              $new_rules = array(
                   'students/([^/]+)/?$' => 'index.php?pagename=$matches[1]&dem=students',
                   'parents/([^/]+)/?$' => 'index.php?pagename=$matches[1]&dem=parents',
                   'counselors/([^/]+)/?$' => 'index.php?pagename=$matches[1]&dem=counsellors',
                   'teachers/([^/]+)/?$' => 'index.php?pagename=$matches[1]&dem=teachers',
              );
              $rules = $new_rules + $rules;
              return $rules;
         }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Query variables and URL rewriting magic’ is closed to new replies.