How can i force Display names to be the same as Usernames?

i need help. My users register to the website. They type their username user_login (see chello) but the Display name (see chello04) is not the same. It has a random two digits at the end.

Is there a way to force Display names to be the same as usernames?

enter image description here

enter image description here

2 Answers
2

You can use the wp_pre_insert_user_data filter.

function wpse_filter_user_data( $data, $update, $id) {
  if( isset( $data[ 'user_login' ] ) ) {
    $data[ 'display_name' ] = $data[ 'user_login' ];
    return $data;
  }
  $user = get_user_by( 'email', $data[ 'user_email' ] );
  $data[ 'display_name' ] = $user->user_login;
  return $data;
}
add_filter( 'wp_pre_insert_user_data', 'wpse_filter_user_data', 10, 3 );

You’ll probably want to use Javascript and/or CSS to hide the field too for a better user experience.

$( '.user-display-name-wrap' ).remove();

.user-display-name-wrap {
  display:none;
}

Leave a Comment