• Resolved lhockley

    (@lhockley)


    As title states. I want to quickly, cleanly just create a very basic plugin (hook/filter) that simply denies the use of space when a new user registers. I understand that I need to use the validate_username hook, but on how to use it to create my small specific tweak I am kinda lost.
    thx

    ps: Stupid idea of WP to allow spaces in username in the first place imo.

Viewing 7 replies - 1 through 7 (of 7 total)
  • This should be pretty easy. You already know the hook, so all you need is some code to remove the spaces. The easy way to do this is like:

    $final = str_replace (" ", "", $username);

    There’s more involved ways of doing this using preg_replace() but regular expressions are pretyt much overkill if all you want is to remove spaces.

    Also, spaces in usernames are not a bad thing. A space is a valid character, just like any other letter or number and can be used just the same. There are some older systems that can’t allow spaces due to the architecture of their software, but there’s not many CMS/blogging systems that I can think of that are like that any more. The only example that I can think of for now that would require no spaces in usernames is a webmail system where your username is your email address. The only reason that people don’t like it is because it’s been drummed into us for so long that you can’t have spaces in usernames that everyone believes that’s the only way to do it now.

    Thread Starter lhockley

    (@lhockley)

    hmm, so… I am very new to using hooks/filters (only written two small plugins so far).

    Here is what I came up with:

    add_filter("validate_username" , "custom_validate_username");
    
    	function custom_validate_username( $username ) {
    	        $sanitized = sanitize_user( $username, true );
    			$final = str_replace (" ", "", $sanitized);
    	        $valid = ( $final == $username );
    	        return apply_filters( 'validate_username', $valid, $username );
    	}

    When the plugin is activated, it gives me an error that the page cannot be found (at registration). The goal was to simply replace the existing function with my own replacement, am I missing something?

    ps, thanks tons for the help thus far.

    Many sites that also deny spaces during registration process.You can remove the any characters that you want. If you want to deny the usage of spaces, just open the plugins of PHP code and search for the piece of codes that contains or holds the value and remove it following from the array.May this idea help you in solving this matter.

    Your function is sort of right, but you’ve actually done way to much. I can see that you’ve copied the existing function and used all of it’s functionality, and that’s not the right way to do it. You don’t replace the function with your own, you just manipualte the output of the function.

    All that you need to do is:

    `add_filter(“validate_username” , “custom_validate_username”);

    function custom_validate_username( $username ) {
    $username = str_replace (” “, “”, $username);
    return $username;
    }`

    Everything else is taken care of by the original function. All that you want to do is remove the spaces, so make sure that you only do that. Another problem with what you had (that is probably the cause of it crashing) is that you’re calling the validate_username filter form inside a function that’s being called from that filter. This gives you an infinate loop of the function calling itself over and over again without an dend until the server kills the process.

    Thread Starter lhockley

    (@lhockley)

    ah, I see now. Thank you much for clearing that up. Was a slight misunderstanding on how hooks work. heh

    Thread Starter lhockley

    (@lhockley)

    hmm, it still doesn’t seem to be working completely. I mean, it’s not crashing anymore, but it is apparently having no effect. Still allowing registration w/ a space.

    Thread Starter lhockley

    (@lhockley)

    ok, well. I figured it out by rethinking the logic. Just simply found some simple regex to see is $username has spaces, then set the $valid parameter to false. Seems to be working. Here is the code:

    add_filter('validate_username' , 'custom_validate_username', 10, 2);
    
    function custom_validate_username($valid, $username ) {
    		if (preg_match("/\\s/", $username)) {
       			// there are spaces
    			return $valid=false;
    		}
    
    	return $valid;
    }

    Thank you so much for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Deny "spaces" in usernames.’ is closed to new replies.