I am creating some project and because of task, I need this:

domain.com/user/user123 - return profile of "user123" with his personal info

domain.com/my-profile - return profile of current logged user

I've tried add_query_args or some tutorials ( likehttp://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/), but it doesn't work for me.

Maybe there is an option with htaccess way, but this part is very confusing me (I mean when I use WordPress way). Any ideas?



If I understand you well, you can do that with add_rewrite_rule(); function to map the url to specific query variables. https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

function myfunc_rewrite_rules() {
  add_rewrite_rule('user/?([^/]*)', 'index.php?pagename=user&username=$matches[1]', 'top');

 function myfunc_username_query_vars($vars) {
  $vars[] = 'username';
  return $vars;

add_action('init', 'myfunc_rewrite_rules');
 //add query vars (username) to wordpress
 add_filter('query_vars', 'myfunc_username_query_vars');

function myfunc_display() {
  $userpage = get_query_var('pagename');
  $username = get_query_var('username');
  if ('user' == $userpage && '' == $username ):
   //show all users
  elseif ('user' == $userpage && '' != $username ):
   //show specific user

 //register plugin custom pages display
 add_filter('template_redirect', 'myfunc_display');

This tutorial can help: http://clivern.com/how-to-add-custom-rewrite-rules-in-wordpress/


After you run this code, you must go to Settings->Permalinks and just hit save button to flush all rewrite rules.(This is the solution for many permalink issues)

  • So, part that shows all users working well, thank you. But I don't know why, WordPress redirect me to main page when I add user123 after /user/ – Doctore Mar 5 '17 at 15:38
  • But this work correctly - domain.com/?pagename=user&username=a and of course this - domain.com/user/?username=a – Doctore Mar 5 '17 at 15:43
  • @Doctore Try flushing rewrite rules by going to Settings->Permalinks and just hit save. then try again – pooria Mar 5 '17 at 15:48
  • @Doctore You're welcome my friend – pooria Mar 5 '17 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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