5

The original urls for users look like /author/login/

Is it possible to replace login by user's id?

In my dream urls should become to /users/34/ (34 is user id).

Thanks.

  • Why do you want to use user IDs instead of user names? IDs are very user unfriendly. – MikeSchinkel Mar 29 '11 at 4:03
7

you need 3 simple functions and hooks

first change the author base:

//change author/username base to users/userID
function change_author_permalinks() {
  global $wp_rewrite;
   // Change the value of the author permalink base to whatever you want here
   $wp_rewrite->author_base = 'users';
  $wp_rewrite->flush_rules();
}

add_action('init','change_author_permalinks');

then add users to query_vars:

add_filter('query_vars', 'users_query_vars');
function users_query_vars($vars) {
    // add lid to the valid list of variables
    $new_vars = array('users');
    $vars = $new_vars + $vars;
    return $vars;
}

then add the new rewrite rule:

function user_rewrite_rules( $wp_rewrite ) {
  $newrules = array();
  $new_rules['users/(\d*)$'] = 'index.php?author=$matches[1]';
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules','user_rewrite_rules');

Now if you don't know how to use this, just copy all of the code and paste in your theme's functions.php file.

  • the last remark is for new users :) – Bainternet Mar 29 '11 at 0:25
  • 5
    You are aware an 'init' hook with a $wp_rewrite->flush_rules() can bring a site to it's knees, right? I just had to do emergency debugging for a client using a plugin that did that; it was taking up to 90 seconds to load a page. Commenting out that line brought page loads down to less than 1 second. flush_rules() need to go in a plugin activation hook, or at least be surrounded by an if (get_option(...)) {...}. – MikeSchinkel Mar 29 '11 at 4:12
  • +1 Mike - Also my concern, i was about to post a similar comment, let's not encourage users to flush rules on init. – t31os Mar 29 '11 at 11:28
  • @Mike , @t31os: So you are saying that $wp_rewrite->flush_rules(); should only run once (say on activation) , and the setup should be run on every init? – Bainternet Mar 29 '11 at 12:00
  • Correct. I've been toying with the idea of using getlastmod() and an option value to run $wp_rewrite->flush_rules(); only once after the .php file is changed; that might be a good way to go that would not require creating a plugin. – MikeSchinkel Mar 29 '11 at 21:28
0

You could use an htaccess rule to rewrite /users/34 to /?author=34

  • 1
    Since there are ways to do this with WordPress plugins I'd personally recommend avoiding the complexity of .htaccess. – MikeSchinkel Mar 29 '11 at 21:47
0

I built a plugin to do this. A friend wanted to hide his author URL's as he didn't like the fact they were displaying his author's usernames. I decided to go one further and allow any user to set their own URL. It redirects their old author pages. https://wordpress.org/plugins/wp-custom-author-url/

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.