Unique nicename for users

This function will make a validation for nicename field so users can only have a unique nicename.

wppb_check_form_field_default-display-name-publicly-as is located in Profile Builder: /front-end/default-fields/display-name/display-name.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Unique nicename for users.
 */
function wppbc_unique_display_name( $message, $field, $request_data, $form_location ){
 
	if ( isset( $request_data['display_name']) )  {
		global $wpdb;
 
		$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->users WHERE display_name = %s", $request_data['display_name'] ) );
 
		if ( $count > 1 )
		 	return __( 'This display name is already in use. Choose another one.', 'profile-builder' );
	}
 
	return $message;
 
}
add_filter( 'wppb_check_form_field_default-display-name-publicly-as', 'wppbc_unique_display_name', 20, 4 );