Halloween Sale 15% off Ends in See the plans
PHP

Require Unique Display Names

Stops users from picking a public display name that another member already uses, which prevents impersonation in comments and forums.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.