Integrating Gravity Forms with Next.js
Fri Jan 17 2025
Headless WordPress
Next.js
Integrating WordPress with Next.js combines WordPress’s content management power with the flexibility of a modern JavaScript framework. For forms, Gravity Forms is a popular, premium solution known for its flexibility. This tutorial shows how to integrate Gravity Forms with Next.js to build a headless contact form using the WordPress REST API.
Prefer to use Contact Form 7? Check out Integrating Contact Form 7 with Next.js.
Gravity Forms
Download Plugin
To get started, ensure you have a Gravity Forms account and license. Download the plugin by navigating to the Downloads section:
Install Plugin
- Open your WordPress Admin Dashboard and go to the Plugins tab in the sidebar.
- Click Add New Plugin, then Upload Plugin. Select the zip file you downloaded in the previous section.
- Once uploaded, click Install Now and then Activate.
Activate License
- Navigate to the Forms tab in the WordPress sidebar.
- Enter your license key in the provided field.
- Click Activate License and complete the initial setup.
Create a Form
- Click Add New, and select Blank Form.
- Name your form.
- Add the desired fields, such as “First Name,” “Last Name,” “Email,” and “Message.”
Enable REST API
- Navigate to Forms → Settings in the WordPress sidebar.
- Go to the REST API tab.
- Enable access to the API. This step allows external applications to interact with your forms.
Next.js
Install Next.js
To create a project, ensure you have Node.js installed and run the following command:
Give your project a name and select the default options for all prompts. Then, wait for the required dependencies to install.
Install shadcn (Optional)
This tutorial uses shadcn to build the form with pre-designed components. To install, run:
Next, install the components needed for our form: button
, input
, textarea
and label
:
Retrieve Form Field IDs
Before creating your form in Next.js, you need to retrieve the field IDs from Gravity Forms to ensure the field names in your code match those in your Gravity Forms form:
- Navigate to the Forms tab in your WordPress dashboard.
- Click on the form you created.
- Select a field to view its ID in the right-hand sidebar.
Build a Contact Form
Create a Contact Form using shadcn components. When defining the form inputs, ensure the name attributes match the field IDs retrieved from Gravity Forms. Use the format input_
followed by the corresponding field ID:
Next, remove the default Next.js boilerplate code from your homepage and replace it with the ContactForm
component:
Start your Next.js app by running:
Go to http://localhost:3000
in your browser to see your form:
Implement Server Action
Before sending your form data to the WordPress REST API via a Server Action, retrieve the ID of your Gravity Forms form:
This ID is required for constructing the WordPress REST API endpoint:
Now, create a file for the Server Action to handle form submissions:
Integrate the sendFormData
Server Action into your ContactForm
component:
After completing the setup, test your form by submitting it. Successful submissions should appear in your Gravity Forms entries:
Handling Validation
Handle Error Responses
Sending incorrect data, such as leaving a required field blank or entering an invalid email address, gives us the following fields (among others) in the response:
Currently, the user isn’t be notified if something goes wrong. We need to display these errors on the front end to address this.
useActionState
Hook
To handle validation messages, we use the useActionState
React hook. This hook helps manage state transitions for server actions, such as displaying validation errors or showing a success message.
To use it, transform the ContactForm
server component into a client component by adding "use client"
at the top of the file.
We also need to make changes to the Server Action. Specifically:
-
Add an extra parameter: We include the
prevState
parameter because it is required by theuseActionState
hook, but we type it asunknown
since it is not utilized. -
Handle errors unrelated to WordPress: We return a generic error saying “An unexpected error has occurred. Please try again later.” in the catch block.
Here’s the updated Server Action:
Now, we can utilize the isPending
variable from our hook to disable the submit button while the submission is being processed:
Display Validation Errors
With the updated Server Action, we can now conditionally render error messages below their respective fields and display the error and confirmation messages at the bottom of the form:
With this changes in place, the form now displays validation messages returned by Gravity Forms:
Preserve User Inputs
When an error occurs, however, all the form fields are cleared, forcing the user to refill the form entirely. This creates a poor user experience. To address this issue, we can send the form data back from the Server Action to the front end whenever there is an error:
With the original form data available on the front end when an error occurs, we can set it as the default value for each field. If the form is submitted successfully, the payload won’t be sent back to the front end, and the fields will be cleared:
Now, the form preserves the user’s inputs whenever an error occurs:
Conclusion
This tutorial covered integrating Gravity Forms with Next.js to create a headless form submission system. By following these steps, you can leverage WordPress’s content management capabilities while maintaining the flexibility of Next.js.
With this approach, you can:
- Streamline form submissions using the WordPress REST API
- Build dynamic forms with Next.js
- Effectively handle validation and user inputs