0

I see a lot of forum threads about the possibility of changing the default WordPress display name to First Name and Last Name. By default, the default display name of my theme is set the First Name and Last Name and I would like to set it back to the login name by default. I'd like to know the bit of code I have to add to functions.php to get the job done. I am using Wordpress 3.8.1

3 Answers 3

2

Based on this this answer:

add_action( 'wp_login', 'wpse_9326315_format_user_display_name_on_login' );

function wpse_9326315_format_user_display_name_on_login( $username ) {
    $user = get_user_by( 'login', $username );

    $userdata = array(
        'ID' => $user->ID,
        'display_name' => $user->user_login,
    );

    wp_update_user( $userdata );

}

As soon as they log-in the settings gets changed. So you not only affect new users but also existing ones.

4
  • You may attach it on 'profile_update' hook, so it will be run on every update to users. Jan 26, 2018 at 7:33
  • Yeah but i think 'profile_update' has no effect on existing users?
    – GDY
    Jan 26, 2018 at 8:42
  • Yes, it has. I use such code in a live website, it is tested . Jan 26, 2018 at 13:47
  • @MarcoMarsala Obviously it has an effect on existing users when they update/edit their profile but does it also fire when they don't edit their profile and just login? If not their names would be in the old display style for as long as they don't edit their profiles.
    – GDY
    Dec 12, 2019 at 7:20
0

Go to Users -> Your Profile Find the section called "Display name publicly as" Select an option that fits your website best.

Update:

Try adding this to your functions.php file and then registering a new user:

add_action('user_register', 'change_default_display_name');

function change_default_display_name($user_id)
{
    $data = get_userdata($userid);
    // check if these data are available in your real code!
    wp_update_user( 
        array (
            'ID' => $user_id, 
            'display_name' => $data->user_login
        ) 
    );
}
9
  • I know that you can manually change the display name of a user but I want all of the registered user to have their name displayed as the login name
    – LaGuille
    Feb 12, 2014 at 22:16
  • 1
    Gotcha! What theme are you using? because the default behavior is to show the user login. So it's likely that the theme has a function inside functions.php that's changing that behavior.
    – gdaniel
    Feb 12, 2014 at 22:17
  • It's a theme I downloaded from templatemonster... I tried to find such function inside my theme's function.php but there was nothing related to display_name
    – LaGuille
    Feb 12, 2014 at 22:26
  • Does the theme have a custom user registration page?
    – gdaniel
    Feb 12, 2014 at 22:28
  • not sure to be honest. I'm kind of just looking for a quick fix to put in functions.php
    – LaGuille
    Feb 12, 2014 at 22:31
0

A better solution is to update the display name every time the user is updated. I'm resorting this old thread because I cannot find this solution anywhere.

/* First name as default display name. */
add_action( 'profile_update', 'set_display_name', 10 );

function set_display_name( $user_id ) {

    $data = get_userdata( $user_id );

    if($data->first_name) {

        remove_action( 'profile_update', 'set_display_name', 10 ); // profile_update is called by wp_update_user, so we need to remove it before call, to avoid infinite recursion
        wp_update_user( 
            array (
                'ID' => $user_id, 
                'display_name' => "$data->first_name"
            ) 
        );
        add_action( 'profile_update', 'set_display_name', 10 );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.