[ad_1]
Is there a plugin that randomly generates username upon registration just like Reddit? or Ai generated username based on the branding of the website.
I tried looking at wordpress plugins page but didn’t find any.
[ad_2]Copyright © 2020 - 2022, Project DMC - WordPress Tutorials for Beginners- All Rights Reserved. Privacy Policy
I tried to edit my comment but something happened with the formatting!
Here is the original comment:
I don’t know of any plugin that does it but there probably is. Otherwise it’s quite an easy implementation if you know a bit of php.
Decide the username structure. I think Reddit usernames consist of a random adjective and a random noun followed by a random number. For example, “Curious_Camel_4392”.
Create your wordlist (Adjective and Nouns) Like this:
$adjectives = array(“Ancient”, “Curious”, “Mysterious”, “Silent”, “Reckless”);
$nouns = array(“Camel”, “Mountain”, “River”, “Tree”, “Cloud”);
Using the word list you made create a function that randomizes your words and adds a few numbers to it. Like this:
function generate_random_username($length = 4) {
$adjectives = array(“Ancient”, “Curious”, “Mysterious”, “Silent”, “Reckless”);
$nouns = array(“Camel”, “Mountain”, “River”, “Tree”, “Cloud”);
$numbers = substr(str_shuffle(“0123456789”), 0, $length);
return $adjectives[array_rand($adjectives)] . “_” . $nouns[array_rand($nouns)] . “_” . $numbers;
}
Create another function that hooks in to the WordPress user registration process and adds a random username according to the previous function. Like this:
function cocio_random_username($username, $raw_username, $strict) {
$random_username = generate_random_username(); // Call the function
while (username_exists($random_username)) {
$random_username = generate_random_username(); // Regenerate if exists
}
return $random_username;
}
add_filter(‘pre_user_login’, ‘cocio_random_username’, 10, 3);
This is just a simple example and not tested. For example the hook pre_user_login runs before a user is created but also when a user is updated. Which would mean that the username will randomize everytime someone updates a user account.
/**
* Generate a random username for new user registration.
*
* @param string $username The username provided by the user.
* @return string The generated random username.
*/
function generate_random_username($username) {
// Generate a random username using current timestamp and random number.
$random_username = ‘user_’ . time() . ‘_’ . rand(1000, 9999);
return $random_username;
}
add_filter(‘pre_user_login’, ‘generate_random_username’);
Put this in function.php and after user creates an account a random username will be applied to it.