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?