I tested the suggestion from my comment and it worked.
Remove the brackets from (myaccount)
and change $matches[2]
in the URL to myaccount
:
function wpse_287203_rewrite_rule() {
add_rewrite_rule( '^user/(\d+)/myaccount/?', 'index.php?pagename=myaccount&user_id=$matches[1]', 'top' );
}
add_action( 'init', 'wpse_287203_rewrite_rule' );
(myaccount)
will not match a whole world. That's a little more involved. See this Stack Overflow answer for how to match an exact word. We don't need to do that here though, since we can just manually put the word into the second argument of add_rewrite_rule()
.
Also, it wasn't part of the question, but it's what comes next. To get access to the user_id
, you need to add it to the list of valid query variables:
function wpse_287203_query_vars( $vars ) {
$vars[] = 'user_id';
return $vars;
}
add_filter( 'query_vars', 'wpse_287203_query_vars' );
Now you can get the user id when on that URL with get_query_var()
:
$user_id = get_query_var( 'user_id' );
$userdata = get_userdata( $user_id );
(myaccount)
and changingpagename
in the URL tomyaccount
:add_rewrite_rule("^user/(\d+)/myaccount/?", 'index.php?pagename=myaccount&user_id=$matches[1]','top');
(Not tested, so not adding as an answer).