Regex in add_rewrite_tag not accepting OR operators?


I am trying to add a custom field to the post permalinks by using a combo of add_rewrite_tag and applying a post_link filter.

What I am aiming for is something like this:

I can manage to get the %mycustomfield% as %psubject% working by using

add_action( 'init', 'register_rewrite_tag');
function register_rewrite_tag() {  
    add_rewrite_tag( '%psubject%', '(.*)' );

However there are only a set number of %psubject% values.

But the regex below, which I have tested on regex testers does not work and instead redirects all posts with this link structure to the homepage as home.php, not even a 404 or index redirect.

add_rewrite_tag( '%psubject%', '(option1|option2|option3)');

However simply having the regex inputs below will work.

(option1) or (.*)

Such as 

will show the correct post.

Edit: I have found even in WordPress 3.6 that this issue still occurs.

It appears that by using the regex OR operator in the add_rewrite_tag function only will allow the last option of the OR statement, but not any before it.


add_rewrite_tag( '%slugname%', '(orange|apple|strawberry)');

Will only allow ‘strawberry’, despite the syntax being a standard OR statement.

This issue does not occur in other WordPress functions such as add_rewrite_rule.

Question: How can I tell the rewrite tag to only allow a set number of strings to be accepted for the permalink?

, , , Alex 10 years 2013-07-26T18:48:38-05:00 0 Answers 86 views 0

Leave an answer