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
remove_action( 'pre_user_login', 'bp_core_strip_username_spaces' );
//add a filter to invalidate a username with spaces
//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 🙂