Skip to:
Content
Pages
Categories
Search
Top
Bottom

Autogenerate or remove username

  • Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    This excellent plugin allows members to log in with their email address and password. A username is no longer required.

    How can I remove ‘username’ from the registration form?

    I want to banish the concept of the username from my system entirely – I effin hate usernames, stupid anonymous usernames are the scourge of the internet, grrr!

    On my network I want people to sign up with email address and full, real name, preferably with seperate fields for first and last name.

    ‘username’ is now a required field and “unrequiring” it probably messes up the system. Is there a way to autogenerate something behind the scenes? Generate it based on the real name?

Viewing 15 replies - 1 through 15 (of 15 total)
  • Avatar of Scotm
    Scotm
    Participant

    @scotm

    I’d like to know this one as well.

    Avatar of Donnacha
    Donnacha
    Participant

    @donnacha

    Interesting, I’ve always thought that signing in with an email address is more elegant that usernames – your email address is unique and you are definitely going to remember it, whereas your preferred username may not have been available when you signed up and, now, you have to remember what you chose instead.

    How hard would it be to have email-as-username added as an option?

    Avatar of Jeff Sayre
    Jeff Sayre
    Participant

    @jeffsayre

    Donnacha-

    You are correct. It should not be that difficult. There are a few special characters in an email address that may need to be properly handled ( the period, the @ sign, potentially a dash in some addresses).

    I suppose you could use htmlentities() to do this, but I don’t see why it would not work. Although I believe WPMU simply escapes the email address on POST.

    Of course, this would not prevent people from entering an all-character username. I guess you’d simply perform the standard check to see if a validly-formatted email address was entered.

    In fact as one of the goals for BuddyPress (and therefore I assume WPMU) is to implement the Open Stack, there will need to be a way for people to signup and sign in using their email address.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Generating the username from the email address is not ideal, because it’s also used for the blog URL – if you allow creation of blogs.

    My first thought would be to generate username from the full name. Or leave the username field on the registration form, but rename it as “blog name” and move it down on the form.

    Or a combination of both where the username/blog name field is not visible when you first register and you get a suggested blog name based on your real name when you register a blog.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Ignore this post! (I had written something else in here previously… but just had a light bulb go off!… love it when that happens!)

    Basically, I need to import a bunch of users from a different system that use their email address as their login.

    I’m going to use WP Email Login as mentioned above.

    But I need to assign a “default” WP username to these users, so what if they do not like that username?

    For this, I’m going to try the WPVN Username Changer plugin and see if this works.

    Will keep you guys posted.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    This function for bp-custom.php adds fullname from xprofile field_1 to wp_usermeta.

    function synchro_wp_usermeta($user_id, $password, $meta) {
    global $bp, $wpdb;

    $fullname = $meta[field_1];
    $space = strpos( $fullname, ' ' );

    if ( false === $space ) {
    $firstname = $fullname;
    $lastname = '';
    } else {
    $firstname = substr( $fullname, 0, $space );
    $lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
    }

    update_usermeta( $user_id, 'nickname', $fullname );
    update_usermeta( $user_id, 'first_name', $firstname );
    update_usermeta( $user_id, 'last_name', $lastname );

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
    }
    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

    It shouldn’t be a big step to make the fullname lowercase, strip the spaces and store it as username.

    Make lowercase and strip space looks something like this:

    $str = strtolower($str);

    $str = str_replace(' ', '', $str);

    But does the system even allow changing the username at the wpmu_activate_user stage?

    The username is stored in four places in the database:

    wp_signups -> user_login

    wp_users -> user_login

    wp_users -> user_nicename (?!)

    wp_users -> user_url (as last part)

    Manually changing the username at these points in the database does NOT screw up my system. Login with email address continues to work fine with the same password. The profile page and links function as normal. :-)

    That is good news. It means it is possible to simply update the username from this same function.

    Now I just have to figure out the PHP and the queries for those four fields. Any pointers apprecriated!

    EDIT: Is $user_id a number or the username? That could complicate things…

    I assume $user_id is the username. Can I make this a two-stage rocket? First run the synchro_wp_usermeta function and then a separate function to deal with username, synchro_name_username? Put them in a plugin with something like this at the bottom?:

    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);
    add_action( 'wpmu_activate_user', 'synchro_name_username');

    Would that execute the functions in the right order?

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Forget the previous post for now. I’m now assuming $user_id is in fact an ID number. So something like this should cover the two most important of the four usernames in the database:

    function synchro_wp_usermeta($user_id, $password, $meta) {
    global $bp, $wpdb;

    $fullname = $meta[field_1];
    $space = strpos( $fullname, ' ' );

    if ( false === $space ) {
    $firstname = $fullname;
    $lastname = '';
    } else {
    $firstname = substr( $fullname, 0, $space );
    $lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
    }

    $lowercase = strtolower($fullname);
    $autousername = str_replace(' ', '', $lowercase);

    update_usermeta( $user_id, 'nickname', $fullname );
    update_usermeta( $user_id, 'first_name', $firstname );
    update_usermeta( $user_id, 'last_name', $lastname );

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_login = %s WHERE ID = %d", $autousername, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_nicename = %s WHERE ID = %d", $autousername, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
    }
    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

    I’ll try that and look into the bp_core_get_user_domain() function after lunch…

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    This basically works!:

    function synchro_wp_usermeta($user_id, $password, $meta) {
    global $bp, $wpdb;

    $fullname = $meta[field_1];
    $space = strpos( $fullname, ' ' );

    if ( false === $space ) {
    $firstname = $fullname;
    $lastname = '';
    } else {
    $firstname = substr( $fullname, 0, $space );
    $lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
    }

    $lowercase = strtolower($fullname);
    $autousername = str_replace(' ', '', $lowercase);

    update_usermeta( $user_id, 'nickname', $fullname );
    update_usermeta( $user_id, 'first_name', $firstname );
    update_usermeta( $user_id, 'last_name', $lastname );

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_login = %s WHERE ID = %d", $autousername, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_nicename = %s WHERE ID = %d", $autousername, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
    }
    add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

    All the usernames in the database get updated to the new username autogenerated from fullname! Even the URL takes the new username.

    Except the user_login in wp_signups. The signups table doesn’t have a user ID. I’ve tried adding an argument $user_email and this line:

    $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_login = %s WHERE user_email = %d", $autousername, $user_email ) );

    But I get a missing argument error and all the entire column of user_logins is reset to the autousername of the latest user. Apparently ‘WHERE user_email = %d’ doesn’t work.

    I’m not sure if I should waste much time on this. Is the user_login in wp_signups used anywhere? It appears in the emails, but those have to be rewritten anyway for email login.

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    I’ve noticed that you have an add_action only when WPMU activates a user… you should also do a check when someone tries to change the full name via BuddyPress – “My Account > Profile > Edit Profile”.

    Not sure what the action would be for that.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Thanks for the suggestion r-a-y.

    The autogenerated username should behave pretty much as the normal username; it shouldn’t be possible to change the username. The URL to the profile page should stay the same, even if the user decides to change his name – which would be a bit unusual anyway.

    Or am I missing some other consequences…?

    Next problem to solve is what to do with the username field on the registration form. It should get an autogenerated temporary value, to be replaced by the function on activation, and be hidden from the user. I found this code to generate a random string:

    $len = 16;
    $base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
    $max=strlen($base)-1;
    $activatecode='';
    mt_srand((double)microtime()*1000000);
    while (strlen($activatecode)<$len+1)
    $activatecode.=$base{mt_rand(0,$max)};

    echo $activatecode;

    To add that as value on the registration form I probably have to hack core file. Moving my attention to rearranging the signup form next…

    Avatar of r-a-y
    r-a-y
    Moderator

    @r-a-y

    Oh sorry! I think I’m confusing what you’re trying to do.

    You’re trying to auto-generate the username based off the display name xprofile field, correct?

    If so, the display name is still editable via the “Edit Profile” screen (like I stated above). Not sure what would happen if you attempted to change the display name… would it sync up with the usermeta table?

    My guess from the post I wrote above is “no”, since you only have your function running when a user has activated their account. But I guess that’s the point!

    Re: rearranging the signup page… BP 1.1 will enable you to move fields around on the sign up page. Check out testbp.org as an example of a customized register page.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    If so, the display name is still editable via the “Edit Profile” screen (like I stated above). Not sure what would happen if you attempted to change the display name… would it sync up with the usermeta table?

    Yes it does, that’s what the original xprofile_sync_wp_profile() already did, although I think you can turn it off in the admin area. Even if you did that, it wouldn’t mess up anything vital.

    My main concern was to consistently get a separate first name + last name in the database, because I need them for several other scripts. That only needs to happen on activation.

    Also I needed to learn how to use the fullname value in a function so I could autogenerate a username from it.

    Re: rearranging the signup page… BP 1.1 will enable you to move fields around on the sign up page. Check out testbp.org as an example of a customized register page.

    I need my site done before Labor Day or September 1st really. And the new more versatile template system does not solve any of my other issues.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    To autogenerate a temporary username put this function in bp-custom.php:

    function randomusername() {
    $length = 10; //length of string
    $start = 65; //Where to start from on ascii table
    $end = 90; //where to end on the table

    for ($k = 0; $k < $length; $k++){
    $randomname .= strtolower(chr(round(rand($start, $end))));
    }

    return $randomname;

    }

    Make this edit to the username input field on the form (hacking core file, should become easier in version 1.1):

    <input name="user_name" type="text" id="user_name" value="'.$user_name = randomusername().'" maxlength="50" />

    Test it if you want; you’ll see a random string in the username field that will be replace by the autogenerated username based on fullname on activation.

    You can hide the username field on the signup form, without getting error messages for not giving it proper TLC, by using this CSS trick.

    That requires another core file hack, since you need to add a id like username-form-field. Again, should become easier in version 1.1.

    My registration form is now:

    Name:

    Email address:

    And that’s it! Everything works. Full name is available as first and/or last name. The URL is the full name lower case without spaces. :-)

    Only thing left to do is clean up the email messages etc. that still mention the username…

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Alternative solution: Use the following Javascript to generate a username on typing:

    function copyinputuser()
    {
    var tmp = document.getElementById('field_1').value;
    tmp = tmp.toLowerCase().replace(/^\s+|\s+$/g, "").replace(/[_|\s]+/g, "");
    tmp = tmp.replace(/[^a-z0-9-]+/g, "").replace(/[-]+/g, "").replace(/^-+|-+$/g, "");
    document.getElementById('user_name').value = tmp;
    }

    And then add onkeyup=”copyinputuser()” to the field_1 input tag, which requires an ugly hack in a core file.

    Avatar of Marc P.
    Marc P.
    Participant

    @mapepro

    Hi
    I know these posts are really old, but does this still work? I’m not quite sure how I really have to do it?
    So can maybe someone help me with it please?
    I want to hide the username field, and would like to auto-generate the username based on the normal “Name” field in BuddyPress.
    So that WP and BP automatically take the first and last name out of the “Name” field. e.g. Name field input: George Smith username: georgesmith

    I already have the plugin activated that user can login with their email address.

    Thanks for help!

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Autogenerate or remove username’ is closed to new replies.