Skip to content

Instantly share code, notes, and snippets.

@tobedoit
Created November 26, 2012 06:43
Show Gist options
  • Save tobedoit/4146888 to your computer and use it in GitHub Desktop.
Save tobedoit/4146888 to your computer and use it in GitHub Desktop.
Wordpress: Set display name with 'first name'
/* Sets the user's display name (always) to first name last name, when it's available **************************************
** http://stackoverflow.com/questions/9326315/wordpress-change-default-display-name-publicy-as-for-all-existing-users *** */
/* !아이디 대신 이름으로 나타내기 ********************************************************************************************* */
/* Sets the user's display name (always) to first name last name, when it's avail. */
add_action ('admin_head','make_display_name_f_name_last_name');
function make_display_name_f_name_last_name(){
$users = get_users(array('fields'=>'all'));
foreach($users as $user){
$user = get_userdata($user->ID);
$display_name = $user->first_name;
if($display_name!=' ') wp_update_user( array ('ID' => $user->ID, 'display_name' => $display_name) );
else wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
if($user->display_name == '')
wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
}
}
@parvizEmami
Copy link

tank you

@angemman
Copy link

angemman commented Jul 1, 2020

It will only show the first name even though I have a last name. Is there anyway to show it as first and last name?

@lucivam
Copy link

lucivam commented Sep 8, 2021

@angemman I did make some modification on code to show first and last name:

// Sets the user's display name (always) to first name last name, when it's available

add_action ('admin_head','make_display_name_f_name_last_name');
function make_display_name_f_name_last_name(){
$users = get_users(array('fields'=>'all'));
foreach($users as $user){
$user = get_userdata($user->ID);

    // get first and last name
$first_name = $user->first_name;
$last_name = $user->last_name;

    // trim to 'full name' string
$display_name = trim( $first_name . ' ' . $last_name );

    if($display_name!=' ') wp_update_user( array ('ID' => $user->ID, 'display_name' => $display_name) );
        else wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
    if($user->display_name == '') 
        wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->display_login) );
}   

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment