BuddyDev

Prevent spaces in WordPress/BuddyPress Usernames

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.

[sourcecode language="php"]

/**
* 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
}

[/sourcecode]

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 🙂

27 Responses to Prevent spaces in WordPress/BuddyPress Usernames

  • Pingback:Недопускаем пробелы в логинах пользователей | Русский BuddyPress и WordPress MS, плагины, шаблоны, хаки

  • This worked great for me. We are running a photo contest on a site with Buddypress. Non-tech users were putting spaces in their user name and having a devil of a time logging in with what they "know" they typed in during registration. Thanks!

  • Hi Brajesh-

    With BuddyPress 1.7-development, the remove_action isn't being called. I hooked the wrapper function to bp_init instead of bp_loaded, and it works as expected. Thanks for the tip.

    • Hi David,
      Thank you. I appreciate the update.

      It seems that the priorities of action have changed in the trunk.

      Will be updating the tutorial once Bp 1.7 beta comes 🙂

  • VIz #

    Thank you, Brajesh Singh, very much appreciated. This is such a dumb thing from BP guys which is on since 6-8 months and no one did anything to fix it….

    • You are most welcome 🙂 Glad that it helped 🙂

    • Is the code snipit above current or do I need to replace all instances of bp_loaded withbp_init? Thanks much.

      • Also…where should I look for the bp-custom.php file? Is it supposed to be in the root buddypress folder or in a subfolder? If the file doesn't exist, where should I place it, in the buddypress folder/file structure? Thanks much.

  • There is no bp-custom.php, is it ok if I paste this to theme's function.php ?

    • Hi Bhavik,
      You will need to create bp-custom.php in your wp-content/plugins directory.

    • Sorry about all of these many messages….

      I have gotten the code to work on my test site by creating the bp-custom.php file and placing it in the plugins directory.

      The only issue I have now, is that my site is in Portuguese and the error message is in English. Is it possible to control the wording of the error message?

      Thanks much

      • Hi Michael,
        you will need to translate that in your BuddyPress Languages file.
        Please take a look here for the details
        http://codex.buddypress.org/developer/translations/
        Hope that helps.

        • Brajesh,

          I am having trouble figuring out which .po file needs to include the translation of this error message. The only place I could find this particular error message is in the buddypress.pot file in the /wp-content/plugins/buddypress/bp-languages folder and it doesn't look like this file is even being used for translation because none of the messages are currently translated. I attempt to include the necessary translation of this particular error message in the file, but I am still seeing the error message on the registration page in english, so it seems like the message must be getting pulled from some other location/file. Any assistance would be sincerely appreciated. Thanks much.

  • Hi Brajesh – would LOVE this functionality.

    Currently using 1.72 and getting this error message:

    Parse error: syntax error, unexpected T_STRING in

    /wp-content/plugins/bp-custom.php on line 33

    Line 33 is

    function bpdev_restrict_space_in_username($valid,$user_name){

    • HI Marbola,
      Please check for the synax issues. If you can post the code on pastebin(all bp-custom.php), I can quickly help you.

  • Great plugin Brajesh…I did place the code in the bp-custom.php but the code shows at the top of my browser… any thoughts?

    • Hi Malik,
      Please make sure to use the php opening tag <?php before anything else in your bp-custom.php

      That will fix the issue.

  • Brajesh, thanks for the quick response…I will purchase your premium option this weekend!!!

  • Update the '<?php' before your code did in fact work…but I had to delete the bp-custom.php file b/c it seemed to cause logout issues where it made the site look as if it was down. Placed bp-custom.php in the wp content/plugins directory and used this initial code:

    When I deactivate then activate the log out issues go away so dont know. thx

  • I was really excited to finally be able to stop new users from registering with usernames which include spaces, but unfortunately I just had to back out the changes. Something about adding the bp-custom.php file to my plugins directory caused a number of problems with my site. These problems included:

    1 – unable to upload new images to my media library
    2 – unable to add images to a post from my media library
    3 – site title in browser window was displayed as "unknown document"
    4 – The "more activity" link on the Buddypress Activity page became inoperable

    🙁

  • You deserve a medal, I couldn't work out what was happening to start with and couldn't believe this was possible when I found out what the problem was.

    Many Thanks

    • Hi Andy,
      Thank you for the generous words. I am happy that it helped.

  • Thanks !!

    thanks, and thanks .. 🙂 😉

  • Thank you very much for this very useful fix! It totally worked on my buddypress site. I am wondering if there is something I can add to this to also prevent characters in usernames? I would like to only allow letters and numbers because characters are also causing problems for me. Any ideas? Thanks again!