Unique display name and nickname in wordpress

Hiya Folks!
We know that display name and nickname are very useful option in wordpress. Some users want to show display name or nickname instead of username. But there is possibility to be the display name and username same for more than one users. It will create confusion in plugin like BuddyPress or Classifieds or so.
But, if you can protect your user and system to create duplicate display name and nickname, then it’s achievable. Let’s make the hands dirty
You can add these codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in their. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// A dirty script by Ash | |
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 = ; $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 this function once | |
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(); | |
} |
The above code speaks a lot!
Many thanks to my colleague Vaughan Montgomery.
Suggestions are welcome
Get an error at this line:
for($i = 0; $i display_name){
syntax error, unexpected ‘display_name’ (T_STRING), expecting ‘;’
Thanks, the code is updated, please try now 🙂
error in SQL QUERY
[Tue Feb 16 09:03:41.435524 2016] [fcgid:warn] [pid 26980] [client 81.106.136.2:55010] mod_fcgid: stderr: WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE user_id = 1 AND blog_id = 1’ at line 1 for query SELECT COUNT(id) FROM WHERE user_id = 1 AND blog_id = 1 made by require(‘wp-blog-header.php’), require_once(‘wp-load.php’), require_once(‘/home/xxxx/wp-config.php’), require_once(‘wp-settings.php’), include(‘/themes/kleo-child/functions.php’), display_name_and_nickname_duplicate_check, wp_update_user, wp_insert_user, do_action(‘profile_update’), call_user_func_array, bp_blogs_add_user_to_blog, bp_blogs_record_blog, BP_Blogs_Blog->save, BP_Blogs_Blog->exists, referer: www.xxxx.com/members/administrator/profile/edit/group/1/
[Tue Feb 16 09:03:41.435695 2016] [fcgid:warn] [pid 26980] [client 81.106.136.2:55010] mod_fcgid: stderr: WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘( user_id, blog_id ) VALUES ( 1, 1 )’ at line 1 for query INSERT INTO ( user_id, blog_id ) VALUES ( 1, 1 ) made by require(‘wp-blog-header.php’), require_once(‘wp-load.php’), require_once(‘/home/xxxx/wp-config.php’), require_once(‘wp-settings.php’), include(‘/themes/kleo-child/functions.php’), display_name_and_nickname_duplicate_check, wp_update_user, wp_insert_user, do_action(‘profile_update’), call_user_func_array, bp_blogs_add_user_to_blog, bp_blogs_record_blog, BP_Blogs_Blog->save, referer: www.xxxx.com/members/administrator/profile/edit/group/1/