0

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.

Igor Beuermann
  • 127
  • 1
  • 10
  • 1
    Try changing your `(.+)` to `([^/]+)` which is “match everything that isn’t a forward slash”. Use that in any area that comes before a forward slash – Chris Haas Jul 07 '21 at 11:36
  • Thanks, @ChrisHaas! It's better now! The rule: add_rewrite_rule('product[/]([^/]+)[/product\-][page/?code=](.+)$', 'index.php?rangeofproduct=$matches[1]&code=$matches[2]', 'top'); now gets both placeholders. In http://example.com/products/led/?code=123456789 rangeofproduct=led and code=123456789. But the 2nd rule rules out the 3rd one although it's completely different. I tried with (\d+) instead of (^/]+) to collect just digits at the end but I lost rangeofproduct. Does the regex checker in PHP differs from e.g. https://regexr.com/? Thank you so much Chris - I'll need to pay somehow all that. – Igor Beuermann Jul 07 '21 at 17:07

0 Answers0