Are you looking for a way to invalidate username with spaces in BuddyPress? By default if a username contains a space during registration, then BuddyPress will automatically replace it with a hyphen. You can override this option by showing an error with usernames having a space. While there’s probably a plugin for this, we have created a quick code snippet that you can use to invalidate username with spaces in BuddyPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
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; }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 27 best WordPress themes for affiliate marketing and 10 best WordPress plugins for web developers.
Comments Leave a Reply