X

Invalidate username with spaces in BuddyPress

Found this great BuddyPress snippet by Remkus de Vries. Normally in BuddyPress when registering if you have a space in the username BuddyPress will automatically replace the space with an _underscore. This snippet rather than adding the underscore will return an error allowing the user to change the username. In my opinion this is a much better option, creating the username with an underscore without notice is not great. Thanks for the great snippet Remkus de Vries checkout his Github profile here.


Code Snippet:


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!