X

Invalidate Username with Spaces in BuddyPress

By default, during user registration, if a username contains a space, BuddyPress will automatically replace it with a hyphen. Using this snippet, you can ask users to revise the username by returning an error rather than adding a hyphen without the user’s consent.

add_action( 'bp_loaded','bpdev_remove_bp_pre_user_login_action') ;
/**
 * 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
 * 
 */
function bpdev_remove_bp_pre_user_login_action(){
  
  remove_action( 'pre_user_login', 'bp_core_strip_username_spaces' );
}
 
add_filter( 'validate_username','bpdev_restrict_space_in_username',10,2) ;
/**
 * add a filter to invalidate a username with spaces
 * 
 */
function bpdev_restrict_space_in_username( $valid,$user_name ){
  
  //check if there is an space
  if ( preg_match('/\s/',$user_name ) )
 
  //if yes, then we say it is an error
  return false;
  
  //otherwise return the actual validity
  return $valid;
}

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!