1

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 );
  }
}
  • 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
2

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.

  • 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
  • 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
  • yea it keeps wanting to pull the display name into the url and i have no clue why. – Noob Theory Aug 1 '13 at 1:48

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.