0

I am trying to get custom rounting on WP without any plugins. I have a bunch of links looks like this: http://example.com/u/UserNickname11

I've created a page with slug = "userinfo" and added such code in function.php of theme:

add_action('init', function(){
    add_rewrite_tag('%username%', '([^&]+)');
    add_rewrite_rule('^u/([^/]+)/?', 'index.php?pagename=userinfo&username=$matches[1]', 'top');
}, 10);

This is not works (showing 404 wordpress page), meantime /index.php?pagename=userinfo&username=3323 works well.

What I am doing wrong?

Added: I made this working by changing permalink options to "default" and back to "custom". And it is works now. But I need an answer - how I can get new custom routes working without making this?

1 Answer 1

2

You have to flush rewrite rules after new rules are added. Visiting the permalinks settings page flushes the rules, which is why it worked after you did this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.