I've created an rewrite endpoint with the add_rewrite_endpoint function … here is the whole contruct:
// Register to query vars
add_filter( 'query_vars', 'add_query_vars');
function add_query_vars( $vars ) {
$vars[] = 'account';
return $vars;
}
// Add rewrite endpoint
add_action( 'init', 'account_page_endpoint' );
function account_page_endpoint() {
add_rewrite_endpoint( 'account', EP_ROOT );
}
// Account template
add_action( 'template_include', 'account_page_template' );
function account_page_template( $template ) {
if( get_query_var( 'account', false ) !== false ) {
return locate_template( array( 'account.php' ) );
}
return $template;
}
This works great so far when i enter a url like example.com/account/username ... but the links in the site are still like example.com?account=username.
How do i redirect from the parameter version to rewritten version? Is it necessary to add a additional rewrite rule or is there any function that these links have to run through?
The account links on the site itself are created by this function:
function account_url( $user_id ) {
$user = get_userdata( $user_id );
return add_query_arg( 'account', strtolower( $user->user_login ), get_home_url() );
}