The usual way to create a user goes with the register_new_user
function. This function offers no filter for the user name. It just checks for uniqueness with username_exists
. To actually create the user register_new_user
then calls wp_create_user
, which in turn calls wp_insert_user
.
The latter function contains the pre_user_login
filter. Then it calls username_exists
again, because the filter may have nullified the uniqueness. If his results in an error, the user is not created and the register_new_user
function will give you Error: Could not register you… please contact the site admin!
So, if you register a user ‘test’ it will pass the first username_exists
, move on to the filter, be modified to ‘prefix-test’ and then pass username_exists
once more. If you register a user ‘test’ again, it will pass the first username_exists
, because there is no user ‘test’, but after the filter it is renamed ‘prefix-test’, which does exist. So it will fail the second username_exists
.
You can get around this by abusing the only filter that is available before the uniqueness test in register_new_user
. That is in sanitize_user
. You can add a prefix there, but beware that this function is called in wp_insert_user
as well, so you will want to add the prefix only when it isn’t already there.