Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#36812 new defect (bug)

WP_Rewrite removes regex groups, should replace with non-capturing

Reported by: cvedovini's profile cvedovini Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.2
Component: Rewrite Rules Keywords:
Focuses: Cc:


I have got that plugin which adds a rewrite tag like so:

add_rewrite_tag('%lang%', '(en|fr)', 'lang=');

One can then customize the permalinks and add the post's language anywhere in the url.

The problem is that, at one point, the WP_Rewrite class adds rules for attachment and on line 1112 of class-wp-rewrite.php it simply gets rid of all the parenthesis in the regex.

So, if I configure the permalink to be /%postname%/lang/%lang%/ the resulting regex for the rewrite rule is:


which is a totally different rule than expected, where anything/lang/en is matched as an attachment page when it really shouldn't be.

Instead the regex should be:


The code on line 1112 of class-wp-rewrite.php should be replaced with:

$submatchbase = str_replace( '(', '(?:', $match);

Change History (1)

#1 @JProffitt71
8 years ago

+1 I just ran into this exact same issue. It is preventing me from properly creating a structure for a rebuild of a site that used month names instead of month numbers in their post permalinks.

I worked around it by defining my own tokens for ( and ), and then replaced those in "post_rewrite_rules"

The only way I could work around it right now is to define my own placeholders for '(' and ')' and replace those in a post_rewrite_rules filter. (Note: I couldn't use non-capturing groups because those throw off the queryvar matching, it puts the next tag in my non-capturing tags query var).

Version 0, edited 8 years ago by JProffitt71 (next)
Note: See TracTickets for help on using tickets.