WordPress.org

Ready to get started?Download WordPress

Forums

Simple Import Users
Strip non-alphanumeric characters from generated usernames (3 posts)

  1. Erin B.
    Member
    Posted 2 years ago #

    Found the answer and figured I would share.

    Go to roughly line 354 or so and find this code block...

    $uarray = split( '@', $user['email'] );
    $user['username'] = sanitize_user( $uarray[0] );

    Now change it to this...

    $uarray = split( '@', $user['email'] );
    $uarray = preg_replace("/[^a-zA-Z0-9\s]/", "", $uarray);
    $user['username'] = sanitize_user( $uarray[0] );

    That's it.

  2. Erin B.
    Member
    Posted 2 years ago #

    Okay, actually this may cause errors in some instances [related post]. Not sure why, but it works most of the time.

    Catchable fatal error: Object of class WP_Error could not be converted to string in... path/to/formatting.php on line 2829

  3. Erin B.
    Member
    Posted 2 years ago #

    Um, it appears I was running that regular expression on the wrong variable. How about this one, which adds an extra step to the WP sanitize_user() process (which leaves those pesky dots in place):

    $uarray = split( '@', $user['email'] );
    $user['username'] = sanitize_user( $uarray[0] );
    $user['username'] = preg_replace('/[^a-zA-Z0-9]/', '', $user['username']);

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic