I have set the display name to show both first name and last name using the function below. How do I manipulate the LAST NAME to only pull the first letter of that string? Example, if you sign up as John Doe (First name= John, Last name= Doe), I want your display name to be John D.


function force_pretty_displaynames($user_login, $user) {

  $outcome = trim(get_user_meta($user->ID, 'first_name', true) . " " . get_user_meta($user->ID, 'last_name', true));
  if (!empty($outcome) && ($user->data->display_name!=$outcome)) {
    wp_update_user( array ('ID' => $user->ID, 'display_name' => $outcome));    

1 Answer 1


Use PHP's substr() function:

  $outcome = trim(
    get_user_meta( $user->ID, 'first_name', true ) 
    . ' ' 
    . substr( get_user_meta( $user->ID, 'last_name', true ), 0, 1 )
    . '.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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