WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am following the WP Example. I know my rewrite rule is being added into wordpress, so I just need to fix the actual rule. So here is my current code:


// Adding a new rule
function mcs_TextbookRewriteRules($rules) {
    $newrules = array();
    $newrules['textbook/(cantonese|mandarin)/([C|M]K?[0-9]+)/([0-9]+)$'] = 'index.php?pagename=textbook/$matches[1]/?cls=$matches[2]&ch=$matches[3]';
    //$newrules['textbook/(cantonese|mandarin)/([C|M]K?[0-9]+)/([0-9]+)$'] = 'textbook/$matches[1]/index.php?cls=$matches[2]&ch=$matches[3]';
    return $newrules + $rules;

// Adding the id var so that WP recognizes it
    function mcs_insertTextbookQueryVars($vars) {
    array_push($vars, 'cls');
    array_push($vars, 'ch');
    return $vars;

I want to map http://localhost/wordpress/textbook/cantonese/CK1/6 to http://localhost/wordpress/textbook/cantonese/?cls=CK1&ch=6

share|improve this question
What you want to map to will not work because it's an intermediate representation. You need to fully map to query parameters off of 'index.php'. Does 'textbook' represent a custom post type, or is it a page with 'cantonese' and 'mandarin' represent a term in a custom taxonomy or a category or a tag? Your regular express references 'C' and 'M' but your example uses 'CK'; please clarify. We need a lot more details about your use-case and your data. – MikeSchinkel Jan 24 '11 at 4:59
@Mike: If there are pages at textbook/cantonese/ and textbook/mandarin/, the pagename parameter can work. – Jan Fabry Jan 24 '11 at 11:09
@Mike - Thank you for pointing that out. I fixed it in the above code. – imHavoc Jan 24 '11 at 21:13
up vote 0 down vote accepted

I see a second ? in your query variables (before the cls query variable), probably from experimenting with the commented-out form, I think you want to use & there.

Your query_vars hook currently adds the id variable, but you use cls and ch in your rewrite rule, so this will not have an effect.

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.

share|improve this answer
Your plugin is pure awesome :). I use it alot. – Patriek Jan 24 '11 at 11:48
Thank you for pointing out in the query_vars, I did not know what it was for. I updated the code and is reflected above. Now textbook/cantonese/CK1/6 is displaying textbook/cantonese/. I had set a if(isset($_GET['cls'])){ echo $_GET['cls']; }. Is there a certain way to call the variable that I am missing? – imHavoc Jan 24 '11 at 21:55
I figured it out that I need to call it using get_query_var(). Thank you for all your help. – imHavoc Jan 24 '11 at 23:48

Your Answer


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.