Require Unique Display Names
Stops users from picking a public display name that another member already uses, which prevents impersonation in comments and forums.
Opens WPIDE on
Code
add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) {
if ( empty( $user->display_name ) ) {
return;
}
global $wpdb;
$taken = $wpdb->get_var( $wpdb->prepare(
"SELECT ID FROM {$wpdb->users} WHERE display_name = %s AND ID != %d LIMIT 1",
$user->display_name,
isset( $user->ID ) ? (int) $user->ID : 0
) );
if ( $taken ) {
$errors->add( 'wpide_display_name_taken', '<strong>Error:</strong> That display name is already used by another member. Please choose a different one.' );
}
}, 10, 3 );
Notes
Checked when profiles are saved in wp-admin; existing duplicates are left alone until those users next save their profile.
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.