I was itching to write this post for long time. Marcos asked this question on our forum around a month ago and informed that the WordPress Restrict Username plugin was not able to prevent the spaces on WordPress standard(not multisite) with BuddyPress activated.
I looked into details, and found that WordPress multisite does not allow spaces/capital letters in the username. WordPress standard has no such restriction.
A little deeper investigation showed that when a user name was being sanitized, BuddyPress was replacing it with hyphens(-). So, It was not the fault of Restrict Usernames plugin. It was not seeing any space at all.
So, I wrote a few line of code(which prevents spaces in user name and you don’t need the restrict username plugin to be active to use it). Here is the code.
/**
* BuddyPress replaces the space with '-' which is not known to the user
* We remove the attached function to stop BP from circumventing the space in username
*/
add_action('bp_loaded','bpdev_remove_bp_pre_user_login_action');
function bpdev_remove_bp_pre_user_login_action(){
remove_action( 'pre_user_login', 'bp_core_strip_username_spaces' );
}
//add a filter to invalidate a username with spaces
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
if ( preg_match('/\s/',$user_name) )
return false;//if myes, then we say it is an error
return $valid;//otherwise return the actual validity
}
You can put the above code in bp-custom.php and It will prevent users from having space in user name.
Please note, If you are on WordPress Multisite with BuddyPress, you don’t need the above code.
Hope it helps you to build a better social network with BuddyPress. Happy Networking
Related posts:
- Playing With buddypress and WordPress, some codes for the site admins
- Creating a Buddypress/WordPress Username availability checker for your site
- Introducing WordPress Restrict Email Domains Plugin
- Enhancing The new User registration Message On WordPress Multisite and BuddyPress to make it more informative for Site Admins
- WordPress Multisite 3.1 and fixing the problems with BuddyPress dependent plugins



Pingback: Недопускаем пробелы в логинах пользователей | Русский BuddyPress и WordPress MS, плагины, шаблоны, хаки
This worked great for me. We are running a photo contest on a site with Buddypress. Non-tech users were putting spaces in their user name and having a devil of a time logging in with what they “know” they typed in during registration. Thanks!
Thank you for the comment Cam.
I am glad it helped
Hi Brajesh-
With BuddyPress 1.7-development, the `remove_action` isn’t being called. I hooked the wrapper function to `bp_init` instead of `bp_loaded`, and it works as expected. Thanks for the tip.
Hi David,
Thank you. I appreciate the update.
It seems that the priorities of action have changed in the trunk.
Will be updating the tutorial once Bp 1.7 beta comes
Thank you, Brajesh Singh, very much appreciated. This is such a dumb thing from BP guys which is on since 6-8 months and no one did anything to fix it….
You are most welcome
Glad that it helped
There is no bp-custom.php, is it ok if I paste this to theme’s function.php ?
Hi Bhavik,
You will need to create bp-custom.php in your wp-content/plugins directory.