I have found quite a lot of post on the subject of WP rewrite but I can't find the right words to describe this problem.
I'm trying to accomplish the following:
mysite.com/examentraining/vak/biologie/havo
To be rewritten to /examentraining/vak?vakname=biologie&level=havo
So when the user types mysite.com/examentraining/vak/biologie/havo
into their adress bar they should be shown the mysite.com/examentraining/vak/
page, but the application should be able use the last two value's between slashes as get parameters (so for the shortcode script the url must look like /examentraining/vak?vakname=biologie&level=havo
)
I have tried the http://codex.wordpress.org/Function_Reference/WP_Rewrite. But the example closest to mine uses an number and only on variable. And at this point the syntax doesn't make enough sense to me to adapt it to my needs. Also when I want to use two variable do I have to call my_insert_query_vars( $vars ) twice?
I hope there isn't a question exactly like mine that I have not found.
EDIT after more than 2 weeks I still have not solved the problem.
I have made the following but it doesn't seem to work, the rules are imported correctly (checked with wordpress plugin, the plugin even says the rule matches the request url). But it doesn't rewrite.
// Adding a new rule
function my_insert_rewrite_rules($rules)
{
$newrules = array();
$newrules['examentraining/training/([0-9]{1,})/?$'] = 'index.php/?p=1320&id=$matches[1]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars($vars)
{
array_push($vars, 'id');
return $vars;
}
I tried one of the simpler examples first, I want this code to rewrite
example.com/examentraining/training/513/ to example.com/?p=1320&id=513
So I want to open the page with the page id of 1320 (this works) and pass the id parameter to this page so my php code can use the variable either via $wp_query->query_vars['trainingid']
or via $_GET['trainingid'] (visiting the url /?p=1320&id=513 works correctly). The only thing that changes in this url is the last part with the id (513 in the example).
Could anybody tell me what is wrong with my rewrite rule?
EDIT 2
I have since read that it is not smart to use such a generic get parameter as id with wordpress so I changed that and I tried another way of getting the page (Als works when called directly from wordpress)
/ Adding a new rule
function my_insert_rewrite_rules($rules)
{
$newrules = array();
$newrules['examentraining/training/([0-9]+)/?$'] = 'index.php?pagename=examentraining/training&trainingid=$matches[1]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars($vars)
{
array_push($vars, 'trainingid');
return $vars;
}
For the sake of testing I flush my rules every init.
I will keep searching but I really hope somebody can point me in the right direction.
/examentraining/vak/
?page
post type page?mysite.com/examentraining/vak/biologie/havo
, and not this/examentraining/vak?vakname=biologie&level=havo
? Your question strongly suggests just the opposite.