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

  • 1
    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. – Evan Yeung Jan 24 '11 at 21:13

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.

  • 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? – Evan Yeung 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. – Evan Yeung Jan 24 '11 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.