Headless WordPress Gravity Forms with Gatsby - Step by Step Tutorial | Rob Marshall (2024)

There are several articles published that describe how to use a headless WordPress install with Gravity Forms on a Gatsby site. However one of the issues I have found is they were written before WpGraphQL for Gravity Forms was released.

I wrote an article back in 2020 that has the same problem. It covers setting up API keys, creating custom lambda functions and a number of other convoluted steps. It was not particularly ‘plug and play’.

This article aims to introduce a far cleaner, simpler way of using Gravity Forms with Gatsby. It will provide a step by step walkthrough of what is needed, as well as provide an example repo for that good old copy/paste.

Note: I have recently written a follow up to this article for Next JS, for those that have moved frameworks.

TLDR

  1. Install wp-graphql-gravity-forms on your WordPress site.
  2. Install gatsby-source-wordpress on your Gatsby site.
  3. Install gatsby-plugin-gravity-forms on your Gatsby site.
  4. Example repo with solution: https://github.com/robmarshall/gatsby-gravity-form-example

Contents

  • What Packages Do I Need?
  • Step 1 – Setup WordPress Side
    • Install WordPress Plugin
    • Limit Submissions to Domain/Cors Issues
  • Step 2 – Install Gatsby Plugins
    • Add the WordPress source plugin to your Gatsby Project
    • Add Gravity Form plugin to your Gatsby project
    • Update your gatsby-config.js file
  • Step 3 – Using the Gatsby Plugin in a Project
    • GraphQL Data and Form Component
    • Add Google reCaptcha to the Form
  • What About Styling?

What Packages Do I Need?

We will use the following packages:

Step 1 – Setup WordPress Side

Install WordPress Plugin

The first step is to get the WordPress site set up. This is pretty painless (much like the whole process tbh).

The aim is to expose your Gravity Forms form data to Gatsby so you can render forms/receive submissions.

  1. Install & activate WPGraphQL on your WordPress site – This can be installed from the plugin directory on your WordPress backend.
  2. Install & activate Gravity Forms (if you haven’t already).
  3. Download zip from the wp-graphql-gravity-forms repository (Direct download link here) and upload it to your WordPress install. Then activate the plugin.

Limit Submissions to Domain/Cors Issues

To make sure that submissions can only come from your frontend domain, and make sure that you avoid any CORs issues from WordPress you can add the following snippet. This should be added to your functions.php file on your WordPress site.

add_filter( 'graphql_response_headers_to_send', function( $headers ) {return array_merge( $headers, ['Access-Control-Allow-Origin' => 'https://yourfrontendurl.com','Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE','Access-Control-Allow-Credentials' => 'true'] );} );

Make sure to update the ‘https://yourfrontendurl.com’ to your actual frontend domain. Make sure that there is no trailing slash.

Step 2 – Install Gatsby Plugins

Add the WordPress source plugin to your Gatsby Project

# Use Yarnyarn add gatsby-source-wordpress# Or NPMnpm i gatsby-source-wordpress

Add Gravity Form plugin to your Gatsby project

# Use Yarnyarn add gatsby-plugin-gravity-forms# Or NPMnpm i gatsby-plugin-gravity-forms

Update your gatsby-config.js file.

{ resolve: "gatsby-source-wordpress", options: { url: "https://yourwebdomain.com/graphql", },},{ resolve: "gatsby-plugin-gravity-forms", options: { // This URL should be the same as you use for your // gatsby-source-wordpress options. url: "https://yourwebdomain.com/graphql", },},

Step 3 – Using the Gatsby Plugin in a Project

From this point onwards you will have access to your WordPress and Gravity Form GraphQl data in your GraphiQL viewer. Try running your develop command and take a look at http://localhost:8000/___graphql.

The exciting bit is plugging our data into the form component!

GraphQL Data and Form Component

There are two steps to rendering a form in Gatsby. The first is getting the data from GraphQl, and the second is placing the form on the correct part of the page.

The below section of code shows an example page, and how you can use the useStaticQuery hook to make this super easy.

import React from "react"import { useStaticQuery, graphql } from "gatsby"import Layout from "../components/layout"import GravityFormForm from "gatsby-plugin-gravity-forms"const IndexPage = () => { // Use useStaticQuery to get the form data for form 1. // There is no need to import the fragment ...GravityFormFields as this is // imported globally by the gatsby-plugin-gravity-forms plugin. const data = useStaticQuery(graphql` query formQuery { wpGfForm(databaseId: { eq: 1 }) { ...GravityFormFields } } `) // Then pass the data into the form component. return ( <Layout> <p>Some intro text here</p> <GravityFormForm data={data} /> </Layout> )}export default IndexPage

Once you have added the above to your project you should be at the position to render forms and send entries to your headless WordPress install.

[support-block]

Add Google recaptcha to the Form

The plugin connects smoothly to the WordPress Gravity Forms install. As long as the reCaptcha details have been set on the WordPress site you could be set with just one small addition to the above code.

As well as the basic query, another smaller graphQl fragment needs adding to pass in your Google reCaptcha v2 site key (Gravity Forms does not support v3 yet).

import React from "react"import { useStaticQuery, graphql } from "gatsby"import Layout from "../components/layout"import GravityFormForm from "gatsby-plugin-gravity-forms"const IndexPage = () => { // Set your form ID in the query below: // Include the wpGfSettings object if you are using reCaptcha. const data = useStaticQuery(graphql` query formQuery { wp { ...GravityFormSettings } wpGfForm(databaseId: { eq: 1 }) { ...GravityFormFields } } `) // Then pass the data into the form component. return ( <Layout> <p>Some intro text here</p> <GravityFormForm data={data} /> </Layout> )}export default IndexPage

Everything else stays as it already was!

What About Styling?

There is no default styling currently packaged in with the gatsby-plugin-gravity-forms plugin. This means that you will need to handle all the styling yourself.

The GravityFormForm component uses exactly the same CSS classes as the PHP Gravity Forms HTML. This means you can move your styling from a pre-existing WordPress site to Gatsby very easily. It doesnt include support for CSS-in-JS/styled components yet.

There is baseline styling CSS file included in the example repo. This should get you to a point where you can see the form working.

Link to example form styling file.

Example repo with solution if you missed it in the article: https://github.com/robmarshall/gatsby-gravity-form-example

Hopefully this article has helped, and if you have any questions you can reach me at: @robertmars

Headless WordPress Gravity Forms with Gatsby - Step by Step Tutorial | Rob Marshall (2024)

FAQs

How to integrate Gatsby with WordPress? ›

Hooking up Gatsby to a new or existing WordPress site takes just a few minutes:
  1. Add the WPGatsby and WPGraphQL plugins to your WordPress site.
  2. Add our WordPress plugin to your Gatsby site.
  3. Configure the plugin with your WordPress credentials & host URL.
  4. Access all your WordPress data using GraphQL queries.

How to create a gravity form in WordPress? ›

Add the form to your site.
  1. Download Gravity Forms. ...
  2. Upload the Gravity Forms plugin to WordPress. ...
  3. Install and activate the plugin. ...
  4. Add the license key. ...
  5. Configure general plugin settings. ...
  6. Create a form. ...
  7. Add the form to your site.
Mar 10, 2022

Why use headless WordPress? ›

In a headless WordPress site, the content is stored in a separate database, which means that it's not exposed to the same security risks as the front-end of the site. Additionally, headless WordPress sites can be more easily updated with security patches, as the front-end can be updated independently of the back-end.

What is the gravity form used for? ›

Gravity Forms is a WordPress plugin that allows you to build complex, accessible forms using an intuitive drag-and-drop builder. Gravity Forms is a premium (paid) plugin (there is no free version!).

How to access Gravity Forms in WordPress? ›

To get started, click on Plugins → Add New from the sidebar menu of your WordPress dashboard. Next, search for “Gravity Forms” on the Plugins page. Click on the Gravity Forms panel from the search results and then choose a purchase option.

Does gravity form have an API? ›

Gravity Forms has an extensive developer API that can be used to access and extend default functionality. Following is a brief description of the different areas of Gravity Forms' API. Understanding the purpose of these different areas is important to make sure the right API is used.

Is Gatsby good for dynamic sites? ›

Although building a dynamic app using Gatsby is possible, the current version of Gatsby (v1) is suitable to build a site that is not “too dynamic” (data changes every 30 mins or so). Furthermore, there are a few features that would be really useful: Faster build times by only rebuilding pages whose data has changed.

How do I add external scripts to Gatsby? ›

Use Plugin You can use gatsby-plugins to add external scripts such as Google Tracking Code, Google Tag Manager, Hubspot code, etc. In order to use gatsby plugin on your website, search for the plugin in gatsby org website https://www.gatsbyjs.org/plugins/ and follow the steps to add it in your site.

How to embed Gravity Forms in WordPress? ›

Here's how you can integrate Gravity Forms into your WordPress website.
  1. Download the Gravity Forms plugin.
  2. Upload the Gravity Forms plugin to WordPress.
  3. Install and activate Gravity Forms.
  4. Activate your license key.
  5. Create your first form.
  6. Add the form to your site.
Oct 26, 2023

Can you use Gravity Forms without WordPress? ›

Gravity Forms requires WordPress to work. To get a Gravity Form onto a site which is not using WordPress, you can host the form on another site (a WordPress site) and iFrame it into your non-WordPress site.

What are the disadvantages of headless? ›

Cons of traditional CMS, decoupled CMS & headless CMS
  • Content types and delivery channels are limited.
  • Limited programming framework.
  • More time and money required for customization, maintenance and enhancements.
Apr 18, 2023

What are the limitations of headless WordPress? ›

Disadvantages of Headless WordPress

The headless approach introduces complexity, necessitating a higher level of developer expertise. Comfort with JavaScript and REST API is crucial. Embracing API-driven content distribution, headless WordPress ensures data flow between the backend and frontend.

Is headless WordPress faster? ›

Many developers believe that headless WordPress can be used to create a much faster WordPress website. By separating the front end of the site using technology such as React. js, many developers try to deliver more efficient code than most WordPress themes.

How to setup Gravity Forms? ›

Download and Install Gravity Forms

From within your WordPress admin dashboard, hover over Plugins on the left side navigation menu and click on Add New. Within this page, click the Upload Plugin button and select the Choose File button.

Is Gravity Forms worth it? ›

However, a lot of people can benefit from the extra features that the Gravity Forms Basic license offers, even if you're just creating simple forms. For just $59 per year, you can access a lot of features to enhance your contact forms, as well as build more advanced types of forms.

Is there a free version of Gravity Forms? ›

Want to try the full Gravity Forms experience… for free? Our Elite demo gives you access to everything - our intuitive form builder, all our advanced features, and any of our impressive add-ons. Find out what else is possible with the Gravity Forms demo… all free, no credit card required.

Where do I put CSS for Gravity Forms? ›

Open field settings: In the Gravity Forms editor, click to open the settings of the field you want to adjust. Add Ready Classes: Under the Appearance tab, find the Custom CSS Class field and enter the relevant Ready Class. To add multiple ready classes, separate them with a space.

How do I test Gravity Forms in WordPress? ›

RUNNING YOUR TEST WORDPRESS AND GRAVITY FORMS
  1. Open your ADMIN area.
  2. Go to FORMS > SETTING > STRIPE and select the TEST radio button at the top. ...
  3. Go to the product page.
  4. Complete all the required fields.
  5. You can add any future date for expiry and any 3 digit number for the CVC.

How many sites use Gravity Forms? ›

Over the course of more than a decade, Gravity Forms has expanded into a robust data management platform powering more than 5 million websites across the globe.

Where is Gravity Forms data stored? ›

When you submit a form in Gravity Forms, it creates an entry. The wp_gf_entry table stores details about the Entry Object. Any entry data for a form is stored in this table.

How do I integrate my website with WordPress? ›

You can add your WP blog to your HTML website through this step-by-step guide:
  1. Install WordPress Blog on Your Web Hosting Platform. ...
  2. Choose a Blog Name and Edit the Theme. ...
  3. Create a New WordPress Page. ...
  4. Create a Posts Page. ...
  5. Set Up Your Menu Links. ...
  6. Publish the Page. ...
  7. Start Blogging!
Mar 19, 2024

How do I integrate a WordPress theme? ›

Install a theme using the upload method in WordPress
  1. Get the theme archive (zip) file. If you have purchased a Premium theme, you must download the zip file from its vendor. ...
  2. Log into WordPress and go to the Theme section. ...
  3. Add new. ...
  4. Upload the installation file. ...
  5. Activate your new theme.

References

Top Articles
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5563

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.