WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Using the below code to chnage the author page url to use the "nickname". Works ok but when the nickname is more than one word it doesnt make it url friendly. Any way to do this?

add_filter( 'request', 'wpse5742_request' );
function wpse5742_request( $query_vars ) {
  if ( array_key_exists( 'author_name', $query_vars ) ) {
    global $wpdb;
    $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta}    WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
    if ( $author_id ) {
        $query_vars['author'] = $author_id;
        unset( $query_vars['author_name'] );    
  return $query_vars; 

add_filter( 'author_link', 'wpse5742_author_link', 10, 3 );
function wpse5742_author_link( $link, $author_id, $author_nicename )  {
  $author_nickname = get_user_meta( $author_id, 'nickname', true );
  if ( $author_nickname ) {
    $link = str_replace( $author_nicename, $author_nickname, $link );
  return $link; 

add_action( 'user_profile_update_errors', 'wpse5742_set_user_nicename_to_nickname', 10, 3 );
function wpse5742_set_user_nicename_to_nickname( &$errors, $update, &$user ){
  if ( ! empty( $user->nickname ) ) {
    $user->user_nicename = sanitize_title( $user->nickname, $user->display_name );
share|improve this question
What isn't "url friendly"? What do want to do instead? – s_ha_dum May 11 '13 at 13:20
two issues- nicknames are not unique, two unique nicknames could clash, post-sanitization. – Milo May 11 '13 at 16:28
The site is very small and admin will fill out nicknames which will be the users company name. @s_ha_dum - im not sure what to do with your question other than to tell you to read what ive already posted. Currently it posts name like "you and me" it needs to be "you-and-me". – Noob Theory May 12 '13 at 13:46
up vote 2 down vote accepted

You've already used sanitize_title once in you code. You need to use that in again, inside the wpse5742_author_link function.

$link = str_replace( $author_nicename, sanitize_title($author_nickname), $link );

That should take care of the spaces. Another other option is to use urlencode but sanitize_title keeps things consistent.

I (minimally) tested your code with that change and it seems to work.

share|improve this answer
Still not getting this to work. Its still showing /author/Test Nick Name and not /author/Test-Nick-Name – Noob Theory Jul 31 '13 at 21:26
Interesting. Try sanitize_title_with_dashes – s_ha_dum Jul 31 '13 at 21:36
still a no go :(. Im trying to use "$current_user->nickname" so perhaps the code above just dont change that function, but only the author link functions? any way to format the nickname call when i need to? – Noob Theory Jul 31 '13 at 21:51
I got it... thanks. – Noob Theory Jul 31 '13 at 21:55
Seems i am having a conflict between the display name and the nickname. Is there a way to lock the display name to whatever the nickname is? – Noob Theory Aug 1 '13 at 1:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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