WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 4 months ago

#36812 new defect (bug)

WP_Rewrite removes regex groups, should replace with non-capturing

Reported by: cvedovini Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.2
Component: Rewrite Rules Keywords:
Focuses: Cc:
PR Number:

Description

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:

[^/]+/lang/en|fr|zh/attachment/([^/]+)/?$

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:

[^/]+/lang/(?:en|fr|zh)/attachment/([^/]+)/?$

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

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

Change History (1)

#1 @JProffitt71
3 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.

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).

Last edited 3 years ago by JProffitt71 (previous) (diff)
Note: See TracTickets for help on using tickets.