1

My WordPress site currently displays users' identifying information by their FIRSTNAME + LASTNAME.

The vast majority prefer to be known by their usernames. I've instructed how to change their "Display Name Publicly As" manually (i.e. via their User settings) but this is less than ideal.

I would like new users to be shown by their usernames as the default. Note that I want this to reflect in several plugins that refer to the "Display Name Publicly As" property.

How can this be done?

  • You could modify your theme to always display the username instead of anything else. – flomei Jan 10 '16 at 21:13
  • It's not only about the theme in this case, it's about a few plugins that I am using as well – CodyBugstein Jan 10 '16 at 21:13
  • That would be a helpful information for your first post, please add that information. – flomei Jan 10 '16 at 21:15
  • @flomei ok I added it – CodyBugstein Jan 10 '16 at 21:16
1

If you want this for all future users then hook into the user_register event and update it there.

Pull the WP_User using get_userdata and wp_update_user info with the new display name.

add_action( 'user_register', 'wpse_20160110_user_register', 10, 1 );

function wpse_20160110_user_register ( $user_id ) {

    // get the user data

    $user_info = get_userdata( $user_id );

    // pick our default display name

    $display_publicly_as = $user_info->user_login;

    // update the display name

    wp_update_user( array ('ID' => $user_id, 'display_name' =>  $display_publicly_as));
}

If you want to set this every login then hook wp_login using PHP_INT_MAX.

function wpse_20160110_wp_login ( $user_login, $user ) {

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

}

add_action('wp_login', 'wpse_20160110_wp_login', PHP_INT_MAX, 2);
| improve this answer | |
  • Thanks! Where should this code go? – CodyBugstein Jan 11 '16 at 19:49
  • Either in a plugin or functions.php, but if you put it in your theme there is a possibility it can be overwritten. – jgraup Jan 11 '16 at 20:28
  • I tried the login portion and it doesn't work to change the display name – CodyBugstein Jan 17 '16 at 6:18
  • I tried the on registration action and it works! – CodyBugstein Jan 17 '16 at 6:23
  • Can you verify the hook is working first? Then make sure the data is coming in correctly. And finally that the priority is late enough? I remember this worked for me when I tested it. – jgraup Jan 17 '16 at 14:12
0

To programmatically set the display name both on registration and profile update, avoiding the infinite loop Betty experienced, you have to check if the display name is already set as needed.

add_action( 'user_register', 'set_login_as_displayname' );
add_action( 'profile_update', 'set_login_as_displayname' );

function set_login_as_displayname( $user_id )
{
    $data = get_userdata( $user_id );

    if ($data->user_login != $data->display_name) {
        wp_update_user( array ('ID' => $user_id, 'display_name' =>  $data->user_login));
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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