Close We have just updated BuddyDev for better performance. If you note a glitch, please let us know by using the feedback button on the left.

BuddyDev

Prevent spaces in WordPress/BuddyPress Usernames

Jotted by Brajesh Singh in Buddypress, Buddypress Tricks on December 18, 2012
18th Dec

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 :)

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Related posts:

  1. Playing With buddypress and WordPress, some codes for the site admins
  2. Creating a Buddypress/WordPress Username availability checker for your site
  3. Introducing WordPress Restrict Email Domains Plugin
  4. Enhancing The new User registration Message On WordPress Multisite and BuddyPress to make it more informative for Site Admins
  5. WordPress Multisite 3.1 and fixing the problems with BuddyPress dependent plugins

9 Responses to Prevent spaces in WordPress/BuddyPress Usernames

Leave a Reply

Your email address will not be published. All the fields marked as * are required.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>