Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Right now you can make your Display Name whatever you want, you can even impersonate an admin. Is it possible to make the Display Name unique?

share|improve this question

3 Answers 3

As far as I'm aware, all you can do is filter the display name via pre_user_display_name and check if it already exists. Unfortunately WP_User_Query doesn't support querying by display_name, so we also have to add to the WHERE clause via pre_user_query. Additionally, there is no elegant way I can think of to handle the case where the display_name already exists beyond returning nothing, since we don't know via the filter what user this potential display name is attached to. or maybe I'm just tired and missing something obvious! Anyway, here's a quick test I created:

class wpa82239_display_name {
    private $display_name;

    public function __construct(){
        add_filter( 'pre_user_display_name', array( $this, 'display_name_filter' ) );
    }

    public function display_name_filter( $display_name ){

        $this->display_name = $display_name;
        add_action( 'pre_user_query', array( $this, 'user_query_filter' ) );
        $args = array(
            'number' => 1,
            'fields' => 'ID'
        );
        $user_search = new WP_User_Query( $args );
        remove_action( 'pre_user_query', array( $this, 'user_query_filter' ) );

        if( 0 == $user_search->total_users )
            return $display_name;

        return null;
    }

    public function user_query_filter( $query ){
        global $wpdb;
        $query->query_where .= $wpdb->prepare(
            " AND $wpdb->users.display_name = %s",
            $this->display_name
        );
    }

}
$wpa82239_display_name = new wpa82239_display_name();
share|improve this answer
    
Hey Milo, thanks for this solution but it's kinda useless if it simply unsets the display_name. Any ideas on this fresh and new day? –  FLX Jan 21 '13 at 1:48
    
I wouldn't say it's completely useless. on the front-end, you can default to login if display name is empty, on the back-end, you can add a bit of javascript via admin_footer to throw a warning up that states they must select a unique display name. –  Milo Jan 21 '13 at 16:11

It's quite dirty, but does the job, just place it in your theme functions.php on a single site, or in a functions.php file inside your mu-plugins folder on a multisite.

The script will also scan existing users (if your site already has many) and any non unique display names will be updated and replaced with username.

I know this isn't totally ideal, but any suggestions for dealing with existing users or improvements are always welcome.

 
    // Make nickname & display_name unique
    // and automatically change non unique nicks & display name to username
    // in case you already have existing users
    // by Ashok & Vaughan Montgomery
    /*
     * adding action when user profile is updated
     */
    add_action('personal_options_update', 'check_display_name');
    add_action('edit_user_profile_update', 'check_display_name');
    function check_display_name($user_id) {
            global $wpdb;
        // Getting user data and user meta data
            $err['display'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users WHERE display_name = %s AND ID <> %d", $_POST['display_name'], $_POST['user_id']));
        $err['nick'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname' AND meta.meta_value = %s AND users.ID <> %d", $_POST['nickname'], $_POST['user_id']));
        foreach($err as $key => $e) {
            // If display name or nickname already exists
            if($e >= 1) {
                $err[$key] = $_POST['username'];
                // Adding filter to corresponding error
                add_filter('user_profile_update_errors', "check_{$key}_field", 10, 3);
            }
        }
    }
    /*
     * Filter function for display name error
     */
    function check_display_field($errors, $update, $user) {
            $errors->add('display_name_error',('Sorry, Display Name is already in use. It needs to be unique.'));
            return false;
    }
    /*
     * Filter function for nickname error
     */
    function check_nick_field($errors, $update, $user) {
            $errors->add('display_nick_error',('Sorry, Nickname is already in use. It needs to be unique.'));
            return false;
    }
    /*
     * Check for duplicate display name and nickname and replace with username
     */
    function display_name_and_nickname_duplicate_check() {
        global $wpdb;
        $query = $wpdb->get_results("SELECT * FROM $wpdb->users");
        $query2 = $wpdb->get_results("SELECT * FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname'");
        $c = count($query);
        for($i = 0; $i < $c; $i++) {
            for($j = $i+1; $j < $c; $j++) {
                if($query[$i]->display_name == $query[$j]->display_name){
                    wp_update_user(
                            array(
                                  'ID' => $query[$i]->ID,
                                  'display_name' => $query[$i]->user_login
                            )
                        );
                }
                if($query2[$i]->meta_value == $query2[$j]->meta_value){
                    update_user_meta($query2[$i]->ID, 'nickname', $query2[$i]->user_login, $prev_value);
                }
            }
        }
    }
    // Call the function
    display_name_and_nickname_duplicate_check();

/*
 * Calling the display_name_and_nickname_duplicate_check() again when a new user is registered
 */
add_action( 'user_register', 'check_nickname', 10, 1 );
function check_nickname() {
    display_name_and_nickname_duplicate_check();
}

share|improve this answer

Try this:

add_action('personal_options_update', 'check_display_name');
add_action('edit_user_profile_update', 'check_display_name');
function check_display_name($user_id) {
global $wpdb;
// Getting user data and user meta data
$err['display'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users WHERE display_name = %s AND ID <> %d", $_POST['display_name'], $_POST['user_id']));
$err['nick'] = $wpdb->get_var($wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname' AND meta.meta_value = %s AND users.ID <> %d", $_POST['nickname'], $_POST['user_id']));
foreach($err as $key => $e) {
// If display name or nickname already exists
if($e >= 1) {
$err[$key] = $_POST['username'];
// Adding filter to corresponding error
add_filter('user_profile_update_errors', "check_{$key}_field", 10, 3);
}
}
}
/*
* Filter function for display name error
*/
function check_display_field($errors, $update, $user) {
$errors->add('display_name_error',__('Sorry, Display Name is already in use. It needs to be unique.'));
return false;
}
/*
* Filter function for nickname error
*/
function check_nick_field($errors, $update, $user) {
$errors->add('display_nick_error',__('Sorry, Nickname is already in use. It needs to be unique.'));
return false;
}
/*
* Check for duplicate display name and nickname and replace with username
*/
function display_name_and_nickname_duplicate_check() {
global $wpdb;
$query = $wpdb->get_results("select * from $wpdb->users");
$query2 = $wpdb->get_results("SELECT * FROM $wpdb->users as users, $wpdb->usermeta as meta WHERE users.ID = meta.user_id AND meta.meta_key = 'nickname'");
$c = count($query);
for($i = 0; $i < $c; $i++) {
for($j = $i+1; $j < $c; $j++) {
if($query[$i]->display_name == $query[$j]->display_name){
wp_update_user(
array(
'ID' => $query[$i]->ID,
'display_name' => $query[$i]->user_login
)
);
}
if($query2[$i]->meta_value == $query2[$j]->meta_value){
update_user_meta($query2[$i]->ID, 'nickname', $query2[$i]->user_login, $prev_value);
}
}
}
}
// Call the function
display_name_and_nickname_duplicate_check();

/*
* Calling the display_name_and_nickname_duplicate_check() again when a new user is registered
*/
add_action( 'user_register', 'check_nickname', 10, 1 );
function check_nickname() {
display_name_and_nickname_duplicate_check();
}

Link: http://bappi-d-great.com/unique-display-name-and-nickname-in-wordpress/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.