HomeGeneral

Prevent spaces and specific unwanted substrings in WordPress usernames

Prevent spaces and specific unwanted substrings in WordPress usernames
Like Tweet Pin it Share Share Email

You can prevent spaces and specific unwanted sub strings in WordPress usernames (restrict usernames wordpress), you have to prepared a regexp for this.

Prepare your regular expression and use the same in the below function.

After that put this function in your theme’s “functions.php” that’s it.

 

//Custom Function
/*
* add a filter to invalidate a username with spaces or have admin in keyword
*/

add_filter(‘validate_username’,’bpdev_restrict_space_in_username’,10,2);

function bpdev_restrict_space_in_username($valid, $user_name){

//check if there is an space in username
if ( preg_match(‘/\s|admin/’,$user_name) )
return false;//if yes, then we can say it is an error
return $valid;//otherwise return the actual validity
}

If you need any further help in this, left your comment.