MyBB Community Forums

Full Version: Auto-generated Username
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I would like the usernames to be automatically generated based on the first and last name the person has given.

Could anybody give me some guidance on how I could go about doing this?

For example:

Peter Johnson has provided his first and last name and the username generated is JOHNPETE. It has taken the first 4 letters from the surname and 4 letters from the first name.
You have to include custom fields as mandatory in registration page then and generate the username for example:

"username" => substr($db->escape_string($user['profile_fields']['fid4']), 0, 4).substr($db->escape_string($user['profile_fields']['fid3']), 0, 4);

^^ in inc/datahandlers/user.php @ line no near about 992
assume your custom field fid3 is firstname & fid4 is lastname

I guess you can handle the rest ...
This is working perfectly fine but just one more question...

How do I make it go through as all in capital letters and how do I add a specific number on the end of the username.

So Peter Jones would turn out like JONEPETE5

Bobby Sinclair would be SINCBOBB5

Betty Harrison would be HARRBETT5

How that makes sense...
$num = 5; // The number to append at the end

"username" => strtoupper(substr($db->escape_string($user['profile_fields']['fid4']), 0, 4).substr($db->escape_string($user['profile_fields']['fid3']), 0, 4)).$num;
Perfect, left positive feedback!

Thank you!