I spent a lot(!) of time getting WordPress and Gatsby working together and then Gatsby improved the integration with gatsby-source-wordpress-experimental
. At the time I wrote this article there wasn't a guide I found sufficient on implementing the new methodology - so here we are.
This tutorial walks you through the basic process of setting up WP and Gatsby and then pulling (post/page) data from WP into Gatsby. Unfortunately, at this time it doesn't include anything beyond this - e.g. adding menus from WP.
- Use an existing WordPress site or setup a new site.
- Download, install, and activate the WPGraphQL plugin from GitHub.
- Download, install, and activate the WP-Gatsby plugin from GitHub.
For step-by-step instructions on setting up a Gatsby environment, I use VS Code to accomplish this quickly.
You'll now need to install gatsby-source-wordpress-experimental
into your Gatsby instance so that Gatsby can use data coming from your WordPress site.
- Run
npm install gatsby-source-wordpress-experimental
- Open
gatsby-config.js
and provide a configuration for the plugin within theplugins: []
section:
{
resolve: `gatsby-source-wordpress-experimental`,
options: {
url: `https://gatsby.wpdeveloping.com/graphql`,
},
},
Lets test to make sure everything is working correctly.
- Run
gatsby develop
. - Wait for Gatsby to spin up.
- Visit http://localhost:8000 to see the Gatsby site.
- Visit http://localhost:8000/__graphql to use GraphiQL, an IDE for writing GraphQL queries.
- Run a test to ensure that your local Gatsby instance can successfully pull data from WPGraphQL.
- You can use this query for pulling Pages and this one for pulling Posts
- Create a
templates
folder inside of thesrc
folder in your local Gatsby instance. - Add a template file
blog-post.js
.
We can programmatically have Gatsby create a page for each blog post pulled from WordPress. This is configured in gatsby-node.js
Pages are stored in the src/pages
folder, we'll be editing index.js
.
You can publish your site by running gatsby build
and then copying the contents of the resulting public folder onto your host. This is super simple (and free) to do with Surge or Netlify, I'd recommend using one of them.
Most of what I write here (at least the good stuff) isn't my own, so check out the partial bibliography for credits / additional resources.