# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: users, profile, display-name
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/display-name-first-last/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Use First and Last Name as the Display Name" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
