Welcome to the second part of the Gravity Forms Front End Login / Register / Edit account series.
In our previous tutorial we have created a simple Gravity based login form. In this tutorial we’ll cover the user registration form.
Part 2 – creating a simple register page
Similarly to the previous form let’s go ahead and create a form with two simple fields:
- Username (with the username CSS class assigned)
- Email (with the email CSS class assigned)
Following the pattern, we’ll need to setup two hooks:
- Which fires during the form validation
- Which fires during the form submission
Gform pre submission hook
This hook fires before the form has been submitted, but after the values have been validated.
Let’s see the code we’ve used for that part:
This hook will do the following:
- Sanitize and store the $_POST superglobal array submitted form fields in the corresponding variables
- Verify if the username and email exist
- If not – create a new user and notify him via email with his automatically generated password
Gform validation hook
Now, we can already register a new user, however the validation rules will not show up if we won’t create them! This means that the form can be submitted even if the specified data would conflict with the existing entries.
The following code will assure us that the form is validated properly:
The above hook is triggered separately for each of the form fields – this is why we need to rely on CSS class names, which we had created before.
This finishes the second part of our tutorial. The user registration is disabled for our site, hence we won’t be posting a working form this time!