0

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.

Thanks

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));    
  }
}
add_action('wp_login','force_pretty_displaynames',10,2); 

1 Answer 1

0

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.