I’d like to be able to use a user’s login name in my URL, and then grab it for use within the page template. Like this:

mywebsite.com/admin/users/user/{user_login}

I’ve tried the following, but the query_var doesn’t get added to the query and the match is always stripped from the end of the URL, like this:

mywebsite.com/admin/users/user/login-name/ ->
mywebsite.com/admin/users/user/

I know the rewrite is happening, because it successfully takes me to the “user” page, but I just can’t seem to get the user login to come with it.

/* add query var */
function add_user_query_vars($query_vars){
    $query_vars[] = 'user_login';
    return $query_vars;
}
add_filter('query_vars', 'add_user_query_vars');
/* add rewrite rule */
function add_user_rewrite_rules($rules){
    $user_rules = array('admin/users/user/([a-zA-Z0-9._-]+)/?$' => 'index.php?pagename=user&user_login=$matches[1]');
    $rules = $user_rules + $rules;
    return $rules;
}
add_filter('rewrite_rules_array', 'add_user_rewrite_rules');

Thanks!

Read more here: WordPress add_rewrite_rule for user login in URL


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , ,

Wordpress related questions and answers: