Force Display Usernames NOT Real Names

I was given the following code a while ago to make sure that my members would not have their real names show up. It worked on our old server, but I have changed servers now and for some reason it is not working, and I have some very unhappy members.

Can you please correct this code to make it work.

// Begin - Force Display Username
function force_pretty_displaynames($user_login, $user) {
$outcome = trim(get_user_meta($user->ID, 'user_login', 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);
// End - Force Display Username

Support Access is granted.
Thank you.

  • Nithin

    Hi Mary,

    Hope you are doing good today. :slight_smile:

    I checked your website, but it seems to display the correct username, instead of the real names. Did you change that manually? For eg: arrowlx is the username I see when I check his profile, and not his real name. Did you fix it?

    I did test your given code, but it seems like the code is not working in my install too. The current code, seems to work when a user logs in. Could you please explain further on how the code had worked before, so that I could check this with a coding expert. Are the users related to BuddyPress, or bbPress?

    Please advise what I'm missing so that I could give a closer look. Have a nice weekend. :slight_smile:

    Kind Regards,
    Nithin

  • Kasia Swiderska

    Hello Mary,

    Can you try this code in your functions.php

    //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->user_login;
    
            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->user_login) );
    
            if($user->display_name == '')
                wp_update_user( array ('ID' => $user->ID, 'display_name' => $user->user_login) );
        }
    }

    this code you would actually run once and then when those display names will be changed you can remove it. If you have many users then it will take a while to change those, but that is one time operation.

    kind regards,
    Kasia

  • Rupok

    Hi Mary,

    The code Kasia gave you above will change display name for all existing user. But if you leave it there permanently, then this code will run every time you load any of your WordPress dashboard pages.

    So once you have run this code, then I'll suggest to remove that code, and create a MU Plugin with the code below. For creating a MU Plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    <?php
    add_action( 'user_register', 'change_display_name_on_registration', 10, 1 );
    function change_display_name_on_registration( $user_id ) {
        $user = get_userdata($user_id);
        $display_name = $user->user_login;
        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->user_login) );
    }

    For getting more idea on mu plugins, you can go through this article: http://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    This code will run every time a new user registers and will change the display name for that new user. I've not tested this but I believe, this will serve your purpose.

    Please let us know how it goes. If you have any confusion or if it doesn't work for you, please let us know. We will be glad to assist further.

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.