I am stuck at my mapping in wordpress.
I have three add_rewrite_rule()
functions:
(1)
add_rewrite_rule('products[\/](.+)[\/]?$', 'index.php?range=$matches[1]', 'top');
add_filter(
'query_vars',
static function ($query_vars) {
$query_vars[] = 'range';
return $query_vars;
}
);
add_filter(
'template_include',
static function ($template) {
$range = get_query_var('range');
if ($range) {
return __DIR__ . '/range.php';
}
return $template;
}
);
(2)
add_rewrite_rule('product[\/](.+)[\/]product][-]page[\/][\?]code=(.+)$', 'index.php?rangeofproduct=$matches[1]&code=$matches[2]', top');
add_filter(
'query_vars',
static function ($query_vars) {
$query_vars[] = 'rangeofproduct';
return $query_vars;
}
);
add_filter(
'query_vars',
static function ($query_vars) {
$query_vars[] = 'code';
return $query_vars;
}
);
add_filter(
'template_include',
static function ($template) {
$varCode = get_query_var('code');
if ($varCode) {
return __DIR__ . '/product-page.php';
}
return $template;
}
);
(3)
add_rewrite_rule('/products[/](.+)[/](.+)[/]$', 'index.php?productsrange=$matches[1]&family=$matches[2]', 'top');
add_filter(
'query_vars',
static function ($query_vars) {
$query_vars[] = 'productsrange';
return $query_vars;
}
);
add_filter(
'query_vars',
static function ($query_vars) {
$query_vars[] = 'family';
return $query_vars;
}
);
add_filter(
'template_include',
static function ($template) {
$varHttpName = get_query_var('family');
if ($varHttpName) {
return __DIR__ . '/family.php/';
}
return $template;
}
);
With the first one I would like catch the range
placeholder (that differ according to different ranges of lights) in links like http://example.com/products/led/, http://example.com/products/halogen/ or http://example.com/products/fluorescent/.
With the second rule I would like to catch placeholders rangeofproduct
and code
as in http://example.com/products/led/?code=123456789.
Here problems start. The rangeofproduct
placehoder never gets any value. The range
placholder still does (I don't know why!). So I could use this one. It maps alright because I use the mapping with $varCode
.
The third rule is probably the most challenging. It seems that the first rule takes precedence and immediately takes the links like the following: http://example.com/products/led/a60-standard-led/. I went online to https://regexr.com/ and indeed the (.+) from the first rule is good enough also for 'led/a60-standard-led'
.
Can anybody help me with this?
The questions are:
(1) Do I need differ the names in placeholders for the same thing (range
, rangeofproduct
, productsrange
).
(2) Why the rangeofproduct
doesn't enter the $query_vars
?
(3) Is it OK if I use matches[1]
in all three rules or should I have in the first rule matches[1]
, in the second rule matches[2]
and matches[3]
and finally in the third rule matches[4]
and matches[5]
?
(4) How should I write regex that would exclude 'led/a60-standard-led'
in the first rule. I tried to find a solution in Regex: match everything but specific pattern (a string containing specific text (say, not match a string having foo) section). But my knowledge of regex is less than basic so I couldn't adapt the solution to my case 'led/a60-standard-led'
excluding '/'
in the middle.
The answer that I wish at most is the answer to the fourth question.