Suggestion for improving the plugin
-
Hello there,
I would like to bring your attention to few details when it comes to username generation.First of all, I have a website with +8000 registered users through Nextend Facebook Connect.
When I checked for usernames that consist of facebook + ID I got +400 results (~5% of the users). For me, this is a problem, and here’s why:
– I’ve set a prefix for new registrations with facebook connect, but these usernames do not follow the prefix convention.
– It’s really not good looking username.So, let’s assume few things before we start:
– The prefix is set to “fb-”
– The user’s First, Last and Full name are all in non-latin language (for example Chinese, Cyrillic etc.)
First name: “Иван”
Last Name: “Иванов”Your code:
$username = strtolower($user_profile['first_name'] . $user_profile['last_name']); // We will end up with $username = "ИванИванов" // As strtolower() is not working with multibyte character encoding it will do nothing in this case $sanitized_user_login = sanitize_user($new_fb_settings['fb_user_prefix'] . $username); // Now we have $sanitized_user_login = "fb-ИванИванов" - so far so goog if (!validate_username($sanitized_user_login)) { // Validity check will fail because strict flag was not passed as a second argument to sanitize_user in the previous line, // but validate_username check against sanitize_user($name, true) $sanitized_user_login = sanitize_user('facebook' . $user_profile['id']); // So at this point we already have a not so good looking username $sanitized_user_login = "faceookID" } // more code
I can offer a fairly simple solution, following these recommendations:
– No matter what always include the prefix. – That’s a must.
– Add additional settings for username_fallback – then check if its set, and if unset, sure go ahead and use the “facebook”. ID approach.
– Remove the spaces, it’s really not a good idea to have spaces in login names.
– Maybe separate the first and last name with an underscore – just an idea, it will be more readable and user-friendly.And here’s the refactored code:
// Demo setup: $user_profile['first_name'] = "Иван"; $user_profile['last_name'] = "Иванов"; $new_fb_settings['fb_user_prefix'] = "fb-"; $new_fb_settings['fb_user_fallback'] = "member"; if (!isset($new_fb_settings['fb_user_prefix'])) { $new_fb_settings['fb_user_prefix'] = 'facebook-'; } // Code $username = strtolower($user_profile['first_name'] . $user_profile['last_name']); $sanitized_user_login = sanitize_user($new_fb_settings['fb_user_prefix'] . $username, true); // do not forget to pass the strict flag (true) // Let's replace the spaces and double underscores with underscore. Its much readable this way. $sanitized_user_login = strtolower(preg_replace("/[\s_]+/", "_", $sanitized_user_login)); // If username consist of 100% non-latin characters, at this point we will end up with the prefix only. if (trim($sanitized_user_login) === $new_fb_settings['fb_user_prefix']) { // Lets use the fallback name // It doesn't really matter if the fallback name is blank/set or not. $sanitized_user_login = $new_fb_settings['fb_user_prefix'] . $new_fb_settings['fb_user_fallback']; } $defaul_user_name = $sanitized_user_login; $i = 1; while (username_exists($sanitized_user_login)) { $sanitized_user_login = $defaul_user_name . $i; $i++; } // At this point, we will have fb-member as username for non-latin usernames // If fb-member is existing: fb-member1 etc. // But most importantly, we will cover edge cases like this: "Michael O'Reilly" // Instead ending up with something like facebook12345678 we will end up with fb-michael_oreilly, fb-michael_oreilly1, fb-michael_oreilly2 etc...
Have a great day!
PS. Here’s a gist with code highlighting:
https://gist.github.com/vdonchev/8c98da9bfcce7c13308f6021b48ed02f
- The topic ‘Suggestion for improving the plugin’ is closed to new replies.