Here at Discover eLearning we are big fans of Gravity Forms! As developers of bespoke digital learning platform utilising WordPress, we love working with the Gravity Forms plugin due to the many professional extensions that exist for the system, as well as the thoroughly detailed documentation guides and knowledge base which allows us to carefully design the flow of data between integral systems connected to a Gravity Form, such as LearnDash LMS or WooCommerce.
With this week’s blog post we would like to share a particularly helpful code snippet which we often use when designing User Registration systems built using Gravity Forms.
Modifying the username auto-generated by the User Registration add-on plugin
The User Registration add-on is one of the most useful extensions available for Gravity Forms, as it allows for a new user action to occur following the submission of any particular form.
When building a standard registration form, for example containing common fields like name, email address, password, etc. it can simply be the case that the user is allowed to enter their own username they wish to use for their account at the same time.
This is not a strategy that we often employ due to the fact that with our own custom-built user profile screens for LearnDash LMS learners, we typically like to ensure that user ID values follow a uniform rule across the platform.
This then makes it easy to publish information screens, like a publicly or privately accessible profile area, which allows inter-connectivity and communication to take place between users and user-groups, based on the ability to include the user ID as a parameter in the page URL.
It is for this reason that we typically follow a rule of the username being created as FIRSTNAME & LASTNAME (with no gap in between).
The default behaviour of Gravity Forms is to concatenate the user’s names together with a period separating the values, but we can modify this easily by including the following code snippet in our child theme’s Functions.PHP file:
add_filter( 'GRAVITY FORM ID GOES HERE', 'auto_username', 10, 4 ); function auto_username( $username, $feed, $form, $entry ) { GFCommon::log_debug( __METHOD__ . '(): running.' ); // Update 1.3 and 1.6 with the id numbers of your Name field inputs. e.g. If your Name field has id 2 the inputs would be 2.3 and 2.6 $username = strtolower( rgar( $entry, '1.3' ) . rgar( $entry, '1.6' ) ); if ( empty( $username ) ) { GFCommon::log_debug( __METHOD__ . '(): Value for username is empty.' ); return $username; } if ( ! function_exists( 'username_exists' ) ) { require_once( ABSPATH . WPINC . '/registration.php' ); } if ( username_exists( $username ) ) { GFCommon::log_debug( __METHOD__ . '(): Username already exists, generating a new one.' ); $i = 2; while ( username_exists( $username . $i ) ) { $i++; } $username = $username . $i; GFCommon::log_debug( __METHOD__ . '(): New username: ' . $username ); }; return $username; }
What makes this code snippet particularly useful is the additional IF statement that will perform the action of adding a numerical value (i.e. 1, 2, 3) to the username just in case it already exists on the platform. Just make sure to replace the values in the first section of the code snippet with the ID value of the Gravity Form itself, and the FirstName + LastName field values which you can find in the Field Setting panel: