Skip to content

Instantly share code, notes, and snippets.

@smeric
Created February 12, 2016 16:34
Show Gist options
  • Save smeric/0df8cac2f07cf9272f95 to your computer and use it in GitHub Desktop.
Save smeric/0df8cac2f07cf9272f95 to your computer and use it in GitHub Desktop.
Username auto generation for Gravity Form User Registration Add-on
<?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