I started work on a new WordPress site this morning and, after creating a couple of users, quickly got bored manually setting each user’s nickname
and display_name
properties in order to display authors as “firstname lastname.” I found a few forum posts in the WordPress support forums for “how do I automatically set the display name for WordPress users?” that had partial answers but no real solutions. Then I stumbled on this article by Rares Cosma that had just what I needed.
Rares’ solution uses the user_register
WordPress action hook, which gets triggered when a new user account is registered. I modified his original version, streamlined it a bit, and applied the ‘firstname lastname’ pattern to both the display name and the nickname
attribute:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
/** * New user registrations should have display_name set * to 'firstname lastname'. This is best used on the * 'user_register' action. * * @param int $user_id The user ID */ function set_default_display_name( $user_id ) { $user = get_userdata( $user_id ); $name = sprintf( '%s %s', $user->first_name, $user->last_name ); $args = array( 'ID' => $user_id, 'display_name' => $name, 'nickname' => $name ); wp_update_user( $args ); } add_action( 'user_register', 'set_default_display_name' ); |
Resources
- Change default display name in the WordPress forums
- WordPress Hooks: user_register by Rares Cosma
- user_register action hook in the WordPress codex
Ibrahim
Is it sheer luck or just plain co-incidence that I was looking for a solution to this exact problem and have provide it just at the right time!!! :D
I didn’t want to modify the core wp files. Thanks heaps! :)
Steve
Glad it helped!
Ashok
You can check this one: http://bappi-d-great.com/unique-display-name-and-nickname-in-wordpress/
Josh
You can also use the pre_user_display_name hook to set the display name on registration. Explained here: http://geektamin.com/blog/533/why-update_user_meta-display_name-doesnt-work-and-how-to-use-pre_user_display_name-instead/
H V S Krishna Kanth
Where should I add this function?
Arif Attari
in theme folder.
function.php file
David Crabill
Here’s code to set the display name to “firstname lastname” and allow the WooCommerce customer to update it: https://github.com/woocommerce/woocommerce/issues/16015#issuecomment-338519085
joemick14
I am using a purchased theme in Word Press that has en error where the byline on an article or blog post displays the Word Press ID name rather than the “Display name publicly as” field. Could someone instruct me on how to add additional CSS code to that section to overwrite this error and display the Display Name rather than the WP ID? Much appreciated!!
Steve
It’s unlikely that CSS is going to fix your problem, here. The template tag used to echo the byline — typically
the_author()
— appears to have been replaced with something likethe_author_ID()
(which would show the author ID instead of the author’s display name).I’d recommend reaching out to the theme author and seeing if they offer support, but without knowing which theme or the domain, generic advice is about all I can offer.
Alex
Hey. Is this still functional? I’ve tried the code several times but it won’t work.
dinislam630
Where should I add this function?please tell me