Email Login & Randomized User URLs – Solution
-
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
- The topic ‘Email Login & Randomized User URLs – Solution’ is closed to new replies.