Rick Jacobson
asked 9 years ago

Hi, I would like to change the username that is displayed on the questions/answers. Right now it says "username" added 6 hours ago". It is pulling the username from the wordpress database. I would like it to pull the first name. This is because most users user their email address for their username and this opens it up to a privacy issue so I just need to tell it to pull the First name, not the username. How can I do that?

1 Answers
DominicStaff
answered 9 years ago

Hi,
About this issue, you can change in the Dashboard > User > Display name publicly as select-box, you can select the First name or last name that you want to display for user.

Or you can add the following code to the functions.php file:

  • 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 . " " . $user->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) );
    }
    }
  • Sets the user’s display name (always) to first 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) );
    }
    }
  • Sets the user’s display name (always) to 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->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) );
    }
    }

Hope this helps !

Powered by DW Question & Answer Pro