Reserving certain usernames/making them unavailable to public
So, I'm playing around with this code snippet with the help of ChatGPT.
Goal: To make certain usernames unavailable to the public
For example, let's say you're running a Notion community and you wanna keep the @Notion (and other brand) username unavailable to the public.
I tested the code in FluentSnippets, and it's working.
The only issue is: I was able to pass through the signup form, even a verification code was sent to my email, and the error message was shown when inserting the verification code. I'd like to do it in a way that the user should not be able to go through the signup page and the error message should be shown on the signup form itself, before sending the verification code.
Any coding expert would like to try debugging this issue?
/**
* Block exact-match reserved usernames during FluentCommunity registration
* and all other WordPress registration flows.
* Prevents sending verification codes to blocked usernames.
*/
// List of blocked usernames (lowercased)
function fecx_get_blocked_usernames() {
return array_map('strtolower', array(
'notion','asana', 'trello'
));
}
function fecx_is_blocked_username($username) {
return in_array(strtolower(trim($username)), fecx_get_blocked_usernames(), true);
}
/**
* π§© 1. FluentCommunity pre-verification validation
* Prevents the email from being sent if username is blocked.
*/
add_filter('fluent_community/validate_registration_data', function ($errors, $data) {
if (!empty($data['username']) && fecx_is_blocked_username($data['username'])) {
$errors['username'] = __('This username is not allowed. Please choose another.');
}
return $errors;
}, 10, 2);
/**
* π§© 2. WordPress core registration fallback hooks (for safety)
*/
add_filter('registration_errors', function ($errors, $sanitized_user_login, $user_email) {
if (fecx_is_blocked_username($sanitized_user_login)) {
$errors->add('username_reserved', __('This username is not allowed. Please choose another.'));
}
return $errors;
}, 10, 3);
add_action('register_post', function ($user_login, $user_email, $errors) {
if (fecx_is_blocked_username($user_login)) {
$errors->add('username_reserved', __('This username is not allowed. Please choose another.'));
}
}, 10, 3);
add_action('user_profile_update_errors', function ($errors, $update, $user) {
if (isset($user->user_login) && fecx_is_blocked_username($user->user_login)) {
$errors->add('username_reserved', __('This username is reserved and cannot be used.'));
}
}, 10, 3);
add_action('user_register', function ($user_id) {
$user = get_userdata($user_id);
if ($user && fecx_is_blocked_username($user->user_login)) {
wp_delete_user($user_id);
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log("Blocked username registration removed: {$user->user_login}");
}
}
});
Already asked ChatGPT to debug/improve the code, a couple of times, but it's unable to do so! π
Possible dumb question as I haven't tested this but what about just manually making dummy WordPress user accounts with those disallowed usernames? Wouldn't that block any new user from signing up with that same username?
Thomas OatesΒ That is exactly how I did it, however I needed to keep them from showing up in the chat. Somehow even if they are not a member they do show up in the chat list.
J VΒ Thomas Oates Not a dumb thing at all, Thomas! In fact, I thought of creating placeholder/dummy accounts in the FC portal! But then those reserved accounts would appear in the members' list. What I'm trying to do is they must not appear anywhere in the community.
This code snippet has almost got me there, it's not allowing to create an account with the reserved usernames. But it's just that I'm trying to enhance it further and show the error message on the signup form.
Also, I have a list of around 200 usernames that I want to reserve.
This is the code you need:
add_filter('fluent_community/reserved_usernames', function ($nams) {
$reserved = ['notion','asana', 'trello'];
return array_merge($nams, $reserved);
});
Shahjahan JewelΒ It's working! π€© You're so amazing, man! πβ¨π
I appreciate how actively involved and dedicated you are to the community. π
Thank you so much, Shahjahan! π
Shahjahan JewelΒ Awesome easy filter. Are all the available hooks documented somewhere?