Created
February 12, 2016 16:34
-
-
Save smeric/0df8cac2f07cf9272f95 to your computer and use it in GitHub Desktop.
Username auto generation for Gravity Form User Registration Add-on
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Username auto generation for Gravity Form User Registration Add-on | |
* | |
* see https://www.gravityhelp.com/documentation/article/gform_username/ | |
*/ | |
add_filter( 'gform_username', 'auto_username', 10, 4 ); | |
function auto_username( $username, $feed, $form, $entry ) { | |
//$username = strtolower( rgar( $entry, '2.3' ) . rgar( $entry, '2.6' ) ); | |
$username = sanitize_user( current( explode( '@', $username ) ), true ); | |
if ( empty( $username ) ) { | |
return $username; | |
} | |
if ( ! function_exists( 'username_exists' ) ) { | |
require_once( ABSPATH . WPINC . '/registration.php' ); | |
} | |
if ( username_exists( $username ) ) { | |
$i = 2; | |
while ( username_exists( $username . $i ) ) { | |
$i++; | |
} | |
$username = $username . $i; | |
}; | |
return $username; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment