Are you looking to restrict usernames in WordPress signup form?
If the registration is open on your website, the user will probably create a username with swear words, trademarks, fake names, and derogatory remarks.
In this quick tutorial, we will learn to restrict usernames in WordPress.
Why restrict usernames in WordPress Sign Up?
People get to pick the username when they sign up on the community and membership websites. You have to allow the registration, but you cannot allow every type of username.
Users can enter usernames similar to admins, moderators, editors, and other WordPress user roles. A user using a username similar to a team member could be confusing and a security risk.
And most terrible, the users could use offensive usernames using racist and misogynist words when making an account on your forum.
You do not want that. Nor can you approve/reject every account manually.
The best way is to block the words and phrases from being used in the usernames. It keeps the community safe from the mischievous crowd.
Restricting Usernames in WordPress with Plugin
The simplest way to restrict certain words and phrases in usernames in WordPress signup is by using the Restrict Username Emails Character plugin.
It is a free WordPress plugin that you can install from the WordPress plugin repository. The plugin allows you to restrict certain words, email addresses, symbols, domain names, and more to be used in signing up.
#1 Install and Activate the Plugin
First, install the plugin from WordPress. Go to the Plugin >> Add New Plugin from the left side panel.
Search for Restrict Usernames Email Characters plugin. Install and activate the plugin.
#2 Restrict Words in Usernames
Once installed and activated, you will find the plugin under the Settings on the left side panel.
The first thing is to make sure the plugin is enabled.
Scroll down to find the ‘Not allow these names’ box.
In the field, enter the words and phrases that you would like to restrict to be used as usernames.
By default, lowercase is equal to uppercase. It means ServerGuy = serverguy. But you can change it from drop down.
Scroll down more to find the ‘Restriction by part (contain,doesn’t contain,starts with,ends with)’ box.
Here you can enter more words. This will prevent the username that contains these words in the username.
Like adding ‘Hitler’ will prevent the username ‘HitlerGood’.
There are more functions to customize the username in WordPress sign up form.
Select whichever you like to turn on.
And that’s it. You have restricted usernames in WordPress with the help of a plugin.
Restricting Usernames in WordPress without Plugin
Restricting username without plugin will need a certain amount of coding skills.
Here is the code snippet:
<?php
function wpsnippet_validate_username($valid, $username) {
$restricted = array(‘add’, ‘your’, ‘words’, ‘here’, ‘In’, ‘this’, ‘format’);
$pages = get_pages();
foreach ($pages as $page) {
$restricted[] = $page->post_name;
}
if(!$valid || is_user_logged_in() && current_user_can('create_users') ) return $valid;
$username = strtolower($username);
if ($valid && strpos( $username, ' ' ) !== false) $valid=false;
if ($valid && in_array( $username, $restricted )) $valid=false;
if ($valid && strlen($username) < 5) $valid=false;
return $valid;
}
add_filter('validate_username', 'wpsnippet_validate_username', 10, 2);
function wpsnippet_registration_errors($errors) {
if ( isset( $errors->errors['invalid_username'] ) )
$errors->errors['invalid_username'][0] = __( 'ERROR: Invalid username.', 'wpsnippet' );
return $errors;
}
add_filter('registration_errors', 'wpsnippet_registration_errors');
?>
You have to add this code to the theme function.php file, which you will find in the Appearance >> Theme Editor.
Add your words to the Array section. Replace these words: ‘add’, ‘your’, ‘words’, ‘here’, ‘In’, ‘this’, ‘format’ from the code.
Editing the theme files is not recommended. Please take a backup before making any changes.
Also Read:
- Core Web Vitals WordPress Optimization Guide
- How to Reduce HTTP Requests in WordPress?
- Top 10 Best WordPress Plugins for Event Registration
- Automatically Log out Idle Users in WordPress
Wrap Up
Running a community forum and group website, it becomes critical to moderate the usernames, as people could be nasty while building them.
The website owner is responsible for keeping the space safe and secure for all users.
I hope this article helped you learn how to restrict usernames in WordPress sign-up. If you encounter any issues, feel free to leave them in the comment section.