Dynamically convert text field for use as user name [RESOLVED]

Hello,

I am trying to find a way to convert a certain value for use as username with the User Registration add-on for Gravity Forms.

I have a registration form where the user can enter a company name and I want to use this company name as username.

My idea was to convert the company name as a “clean” version and put it in a hidden field that is used as username.

So have to convert all spaces to “-” (hyphens) and convert all special characters like umlauts etc. (i.e. ÄÜÖäüöÅåèç) and make all lowercase. So if possible ä would be converted to ae and ü to ue and è to e.

If someone enters “John Doe & Sons with Björk et garçon” it should be converted to “john-doe-sons-with-bjoerk-et-garcon”. I guess hyphens are allowed as WordPress username.

Is that possible somehow?

The other way would be to allow all special characters in the username field.

Thanks a lot for your help,
Michael

UPDATE:
Roxy from GravityWiz.com sent me a snippet I can use with the “GP Copy Cat” plugin from GravityWiz.com.

I activated the “GP Copy Cat” plugin and added the css class “copy-3-to-5”. 3 is the ID of the company name field and 5 is the ID of the target field.

I also replaced the “1” in the code with the target field ID “5” in my form.

You can also add other Unicode characters in the code (i.e. u0026 for &).

/**
* Gravity Perks // Copy Cat // Normalize Special Characters
* https://gravitywiz.com/documentation/gravity-forms-copy-cat/
*
* Use this snippet to normalize special characters into their ASCII equivalent.
*
* Instructions:
* 1. Install our free Code Chest plugin.
* Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
* 2. Copy and paste the snippet into the editor of the Code Chest plugin.
* 3. Update the target field ID within the snippet.
*/
gform.addFilter( 'gppc_copied_value', function( value, $elem, data ) {
	// Update "1" to the ID of the field being copied to.
	if( data.target == 1 && value ) {
		var combining = /[\u0300-\u036F]/g; 
		value = value.normalize('NFKD').replace(combining, '')
	}
	return value;
} );

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Just a note for other folks looking to do the same… we eventually landed on an even simpler solution that required no extra fields.

add_filter( 'gform_username', function( $username ) {
    return sanitize_user( $username );
} );

It simply sanitizes the username on submission, prior to validation.

1 Like