Use First and Last Name as the Display Name
Sets each user's public display name to "First Last" whenever both names are filled in, instead of showing their username.
Opens WPIDE on
Code
add_filter( 'wp_pre_insert_user_data', function ( $data, $update, $user_id, $userdata ) {
$first = isset( $userdata['first_name'] ) ? trim( $userdata['first_name'] ) : '';
$last = isset( $userdata['last_name'] ) ? trim( $userdata['last_name'] ) : '';
if ( '' !== $first && '' !== $last ) {
$data['display_name'] = sanitize_text_field( $first . ' ' . $last );
}
return $data;
}, 10, 4 );
Notes
Applies when a user is created or their profile is saved; existing users change on their next profile update.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.