Skip to:
Content
Pages
Categories
Search
Top
Bottom

Email Login & Randomized User URLs – Solution

  • Avatar of Nick Watson
    Nick Watson
    Participant

    @nickbwatson

    Hey, I just wanted to post the solution that I used in a fresh topic so that I could post it on http://buddypress.org/forums/topic/faq-how-to-code-snippets-and-solutions.

    (this is the solution to http://buddypress.org/forums/topic/email-login-random-member-urls)

    Before I start I want to thank Peterverkooijen for helping me with this.

    So this takes a few steps, but I would think others would be interested in this.

    1. Download and install the wp-email-login plugin.

    -this allows users to login with their emails

    2. Save the following code in a file called usernameGen.js somewhere where you can direct to.

    function copyinput(){
    var tmp = document.getElementById('signup_email').value;
    tmp = tmp.toLowerCase();
    tmp = tmp.replace(/[^a-z0-9-]+/g, "").replace(/[-]+/g, "-").replace(/^-+|-+$/g, "");
    var chars = tmp;
    var string_length = 20;
    var scramble_string = '';
    for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    scramble_string += chars.substring(rnum,rnum+1);
    }
    var number_chars = "0123456789";
    var number_length = 5;
    var scramble_number = '';
    for (var i=0; i<number_length; i++) {
    var rnum = Math.floor(Math.random() * number_chars.length);
    scramble_number += number_chars.substring(rnum,rnum+1);
    }

    document.getElementById('signup_username').value = "mem"+scramble_number+scramble_string;
    }

    -I got the base for my code from this post

    -This code takes the email a user types in on the registration page and scrambles the characters of the email into a 20 character string. It generates a randomized 5 digit number and it puts “mem” for member at the start of your URL.

    3. In your header.php within the head tag put in:

    <script type="text/javascript" src="usernameGen.js"></script>

    -but you will need to change (src=”usernameGen.js”) to the directory you saved your js file in.

    4. In register.php change

    <label for="signup_username"><?php _e( 'Username', 'buddypress' ) ?> <?php _e( '(required)', 'buddypress' ) ?></label>
    <?php do_action( 'bp_signup_username_errors' ) ?>
    <input type="hidden" name="signup_username" id="signup_username" value="<?php bp_signup_username_value() ?>" />

    <label for="signup_email"><?php _e( 'Email Address', 'buddypress' ) ?> <?php _e( '(required)', 'buddypress' ) ?></label>
    <?php do_action( 'bp_signup_email_errors' ) ?>
    <input type="text" name="signup_email" id="signup_email" value="<?php bp_signup_email_value() ?>" />

    to

    <input type="hidden" name="signup_username" id="signup_username" value="<?php bp_signup_username_value() ?>" />

    <label for="signup_email"><?php _e( 'Email Address', 'buddypress' ) ?> <?php _e( '(required)', 'buddypress' ) ?></label>
    <?php do_action( 'bp_signup_email_errors' ) ?>
    <input type="text" name="signup_email" id="signup_email" value="<?php bp_signup_email_value() ?>" onkeyup="copyinput()" />

    -I removed the “Username (required)” and the “bp_signup_username_errors”

    -and I also added onkeyup=”copyinput()” to the email input so that while a user types in this field, it calls upon our “copyinput” function within our js file.

    And that’s it, no core files were edited!

    I know it’s a little complicated but if you follow the instructions closely than it should work for you.

    So here is what it does, and what it did.

    What it USED TO DO

    When a user goes to register, they would type in a username, an email and a password.

    Their username becomes their URL and that’s the name they used to login.

    So JoeSmith’s URL is

    http://www.yoursite.com/members/joesmith

    What it DOES NOW

    When a user goes to register, they get to type in an email and a password.

    Their ‘username’ field is hidden.

    The code will randomly generate a URL for the person like below:

    mem12345abcdefghijklmnopqrst

    so their URL would be

    http://www.yoursite.com/members/mem12345abcdefghijklmnopqrst

    Freeing up the good names like John and Smith to be created manually by an administrator.

    There is a small small small chance that it will randomize the code to EXACTLY what someone’s URL already is, BUT then if that happens:

    normally a message would pop up and say “this username is taken”, but because I’ve hidden the username error box nothing pops up. However the page is refreshed, allowing the user to retype something into the email section, re-randomizing the URL.

    If you have any questions or you have a better way of doing this, let me know.

    I felt like it was my turn to give something back to buddypress, this forum has done so much for me.

    Thanks

    Nick

Viewing 8 replies - 1 through 8 (of 8 total)
  • Avatar of osasko
    osasko
    Participant

    @osasko

    Awesome. Thanks!!

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Thanks for the mention nickbwatson. I did not write the code, it was assembled from several sources with help from BP developers.

    In my version I generated the username/user URL from the fullname, onkeyup on the form. I use the same javascript trick for blog name. I think there were some security objections to doing that, but it works for me for now.

    Someone earlier also posted a simpler php version that produces an entirely random string as value for username on a hidden input field. Can’t find that post…

    Avatar of Nick Watson
    Nick Watson
    Participant

    @nickbwatson

    @osasko: Glad I could help

    @Peterversooijen: It’s really no problem, I wouldn’t have been able to do this at all with the code you provided. That’s what I like about buddypress, there’s no secrets, it’s just everyone helping everyone.

    I’m sure there are some better ways to do this, but like you said, it works for me for now.

    If I can find the other simpler php version I’ll post it here, or if anyone else does that’d be great.

    Avatar of osasko
    osasko
    Participant

    @osasko

    Hi, I think I’ve done everything correctly.

    On my register page – the username is removed , but when I say “complete sign-up” , the page just remains on the register page and no registering takes place.

    I used normal windows notepad to create the javascript file and saved it in my registration folder of my own theme.

    Don’t know?

    Avatar of osasko
    osasko
    Participant

    @osasko

    Would this still work with 1.2?

    Think maybe I am not getting it right because of BP 1.2?

    Avatar of sweller
    Sweller
    Participant

    @sweller

    I was looking for the same solution and appreciate all the work you guys have done. However the javascript solution seems like a long way to generate a random string. Is there a reason not to use the mt_rand function or something similar? So to add “mem” the username form would look like:

    <input type="hidden" name="signup_username" id="signup_username" value="<?php echo("mem" . mt_rand()) ?>" />

    Avatar of osasko
    osasko
    Participant

    @osasko

    Sweller , bit confused now….

    Do you just have to replace the 3 lines that relate to “username” in your register.php , with the line you provide and not do anything else?

    Avatar of sweller
    Sweller
    Participant

    @sweller

    Yeah, it seems to work fine by replacing the three lines of the username block with mt_rand. However be sure that there are letters somewhere in the value, such as the “mem” above. Buddypress will throw back an error without letters.

    Also this probably wouldn’t work with large websites with thousands of people, as the chance of generating the same username goes up. You’d probably have to write a function that generates a long string of random letters and numbers.

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

You must be logged in to reply to this topic.