Frage

I am not a great regex mind, but I feel like that is the problem with my code here, and wonder if someone can lend better eyes.

The code is this bit of PHP...

function add_results_rewrite_tags() {
add_rewrite_tag('%resultant%','([^0-9]+$)');
}
add_action( 'init', 'add_results_rewrite_tags' );

function add_results_rewrite_rules() {
add_rewrite_rule('^([^&]+)/your-result/([^0-9]+$)/?$','index.php?    
name=$matches[1]&resultant=$matches[2]','top');
}
add_action( 'init', 'add_results_rewrite_rules' );

What I expect to happen is that a URL like http://deja1987shadow.wpengine.com/test-post/your-result/five/ will result in a 404 not found error, while a URL formatted as http://deja1987shadow.wpengine.com/test-post/your-result/5/ will return a found page.

However, I actually get the opposite results here... URLs ending with /five/ (or other text) will come up with a 'hit', while the numeric endings like /5/ will come up 404!

It seems to me like I must be doing my regex patterns wrong, but I can't see why I'm so wrong as this. It's possible I'm also using these wordpress functions wrong somehow, but I feel like I've got that part figured out well enough...clearly I don't get a 404 when some URLs contain /your-result/ in them, which is really what these functions are meant to do. Now I just want to make sure that only numeric values of "resultant" result in hits.

Any ideas? Thanks!

War es hilfreich?

Lösung

I don't think the first part of your regex will match the post url. I'm assuming the first match is alphanumeric. Also, this is an Apache RewriteRule so there won't be a $matches variable, just a regex placeholder.

function add_results_rewrite_rules() {
    add_rewrite_rule(
        '^([A-za-z\-]+)/your-result/([0-9]+$)/?$',
        'index.php?name=$1&resultant=$2',
        'top'
    );
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top