Rohit D
asked 9 years ago

I am using buddypress username only plugin which works fine everywhere else except dwqa section. How do i change the user’s dispay name to his unique username on my website ?

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 !

rd08
replied 9 years ago

I want to have the username as default instead of the first name / last name.
Eg – @xyz is the username used by a user while signing up apart from his her full name.

dominic Staff
replied 9 years ago

At the moment, our plugin does not support to display author name like that, unfortunately.

Travis
replied 5 years ago

Is this still not supported? Or is there an updated code to add to functions.php?

Dominic Staff
replied 5 years ago

Yes, you can try this solution, you can add the code to the functions.php file.
If you have any issue or question, you can let me know. We will check and help you resolve it.

Trav
replied 5 years ago

Hello, thanks for the answer. I was wondering if the show “Username” as default display name is supported?

Dominic Staff
replied 5 years ago

At the moment, the plugin will show the username. If you want to show the last name, first name or the full name you can use the code that we have provided in this question.

Powered by DW Question & Answer Pro