• Resolved ZeroGravity

    (@zerogravity)


    I would like to generate the username on the registration form and have it readonly. I tried to see if I could use wpmem_register_form_rows but without success. Is there a way to populate the username on registration?

    This is the code I tried using when testing. I would be randomly generating the username. The fixed text in the example was just for testing.

    add_filter( 'wpmem_register_form_rows', 'my_new_element', 10, 2 );
    function my_new_element( $rows, $tag ) {
        $rows['username']['value'] = "fluffybunny";
        return $rows;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • Chad will give you a better answer but this is what I learned from generating usernames.

    On initial load, I generated unique usernames from a member’s first name and street address and loaded them with all the user’s other details from a CSV file via the plugin “Import Users from CSV”. This process worked well for the initial load but not so well over time. The plugin has a learning curve in terms of structuring the CSV file and its column names but once past that it is great.

    Existing Users forgot their usernames and had to be reminded of how it was constructed in order to log in. New users filled out a registration form and had to be instructed how to build their username.

    The process was complicated because the site supports a couples club where some couples may be married and live at the same address and others are partners living at different addresses. Some share one email address, others have individual email addresses. So I decided on one member record for each member of a couple.

    After a couple of years, I was spending way too much time answering log on problems. Also, because members renew annually, every January I have to deactivate non-renewers so that was a good time to re-think the process.

    I made the username the member’s email address. For those without an email address (most couples don’t want two copies of every email), I created a fake email address in the form [email protected]. Since you can’t change usernames I had to delete everybody (except the Admin; that is embarrassing since it locks you out and one has to restore a backup). Then I reloaded the newly active members with their email addresses as their usernames, again using “Import Users from CSV”.

    Since this is a couples club, generally one member of a couple does the data entry so this year I changed that process so that the typing member registers with their data and then repeats the registration for the second member of the couple. Much simpler…

    One drawback: I have to delete the fake email addresses before creating an email blast.

    Now there is no confusion. All login screens will accept the user’s email address. But guess what? They get a new email addresses and then call me to find out why they can’t log on…

    Thread Starter ZeroGravity

    (@zerogravity)

    Thanks for the reply @xbootenk with your experience. I am in a situation where I need usernames that will not identify the user and I can’t trust users to do that. ?? Users will be able to login with either their username or email address so hopefully they won’t forget that.

    There won’t be a lot of members and the site may only be active for up to 12 months. Because of this I shouldn’t have too many issues maintaining user accounts.

    I found a way using wpmem_register_form and str_replace but doing it this way makes me cringe. I’m hoping there maybe a way that is less prone to error.

    Thread Starter ZeroGravity

    (@zerogravity)

    Just realized I didn’t post my code as a solution to what I was trying to do. So ‘ere it is.

    add_filter( 'wpmem_register_form', 'zgwd1010_register_form_filter', 10, 4 );
    function zgwd1010_register_form_filter( $form, $toggle, $rows, $hidden ) {
    	$username = createRandomString( 8, 'ALPHANUM' ) . ".trh";
    
    	while ( username_exists( $username ) ) {
    		$username = createRandomString( 8, 'ALPHANUM' ) . ".trh";
    	}
    
        return str_replace( 'id="username" value=""', 'id="username" value="' . $username . '" readonly="readonly"', $form);
    }

    The createRandomString function does what it says. Creates a random string with a length of 8 characters.

    rongenius757

    (@rongenius757)

    What file does the code go in? Thank you so much for your question, answer and help so far.

    Thread Starter ZeroGravity

    (@zerogravity)

    You can put it in your themes functions.php or create your own plugin so it’s theme independent. Did you want the code for the createRandomString function? It’s a simple one and there are many other examples across the internet you can use.

    rongenius757

    (@rongenius757)

    Yes please what is the code? Also, is there an option in the code to make it alphanumeric or to me the string all numbers?

    Thread Starter ZeroGravity

    (@zerogravity)

    Here’s the code. It has a multitude of options for constructing the random string. You can also define which characters to use in the constants.

    /**
     * Generate a random string
     *
     * Length of string to generate
     * Type of characters to use
    **/
    function createRandomString($length = 8, $type = "") {
    	$alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    	$numbers = "1234567890";
    	$symbols = "!@#$%^*()+?";
    	
    	switch(strtoupper($type))
    	{
    		case 'ALPHANUMSYM': $charString = $alphabets . strtolower($alphabets) . $numbers . $symbols; break; 
    		case 'ALPHANUM': $charString = $alphabets . strtolower($alphabets) . $numbers; break;
    		case 'UPPERALPHANUMSYM': $charString = $alphabets . $numbers . $symbols; break;
    		case 'LOWERALPHANUMSYM': $charString = strtolower($alphabets) . $numbers . $symbols; break;
    		case 'ALPHASYM': $charString = $alphabets . strtolower($alphabets) . $symbols; break;
    		case 'ALPHA': $charString = $alphabets . strtolower($alphabets); break;
    		case 'LOWERALPANUM': $charString = strtolower($alphabets) . $numbers; break;
    		case 'UPPERALPHANUM': $charString = $alphabets . $numbers; break;
    		case 'NUMSYM': $charString = $numbers . $symbols; break;
    		case 'LOWERALPHASYM': $charString = strtolower($alphabets) . $symbols; break;
    		case 'UPPERALPHASYM': $charString = $alphabets . $symbols; break;
    		case 'LOWERALPHA':$charString = strtolower($alphabets); break;
    		case 'UPPERALPHA': $charString = $alphabets; break;
    		case 'NUM': $charString = $numbers; break;
    		case 'SYM': $charString = $symbols; break;
    		default: $charString = $alphabets . strtolower($alphabets) . $numbers . $symbols;
    	}
    	 
    	srand((double)microtime() * 1000000);
    	$count = 1;
    	$randomString = "";
    	 
    	while ($count <= $length) 
    	{
    			$startChar = rand(0, strlen($charString) - 1);
    			$tempChar = substr($charString, $startChar, 1);
    			$randomString = $randomString . $tempChar;
    			$count++;       
    	}
    	
    	return $randomString;
    }
    rongenius757

    (@rongenius757)

    Phenomenal. Thank you sooo much! So that code goes in the theme’s function.php file along with your first code that I pasted below? Also, where you put ALPHANUM between the single quotations in your first code shown below is where I put my option that I choose from the options listed underneath “switch(strtoupper($type))” in your last code that you posted yesterday?

    add_filter( ‘wpmem_register_form’, ‘zgwd1010_register_form_filter’, 10, 4 );
    function zgwd1010_register_form_filter( $form, $toggle, $rows, $hidden ) {
    $username = createRandomString( 8, ‘ALPHANUM’ ) . “.trh”;

    while ( username_exists( $username ) ) {
    $username = createRandomString( 8, ‘ALPHANUM’ ) . “.trh”;
    }

    return str_replace( ‘id=”username” value=””‘, ‘id=”username” value=”‘ . $username . ‘” readonly=”readonly”‘, $form);
    }`

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Generate username on registration form’ is closed to new replies.