This guide will walk you through setting up a headless WordPress app using SnapWP.
- PHP: v7.4+
- WordPress: v6.7+
- A Block Theme
-
Install and activate the latest release versions of the following plugins:
With WP-CLI:
You can install the latest versions of the required plugins using the WP-CLI command below:
wp plugin install wp-graphql https://github.com/wpengine/wp-graphql-content-blocks/releases/latest/download/wp-graphql-content-blocks.zip https://github.com/rtCamp/snapwp-helper/releases/latest/download/snapwp-helper.zip --activate
-
(Optional) If you're running your WordPress site on a different domain than your frontend, you may need to configure CORS headers.
- Node.js: v22+ (with
npmandnpxinstalled). - A WordPress backend configured with SnapWP Helper (see previous section).
To create a new headless WordPress app using SnapWP, follow these steps:
-
Run the scaffolding wizard:
npx snapwp
-
Answer the CLI prompts:
-
Enter the path to the directory where you want to create the app, e.g.
./my-headless-app -
Create an Environment File:
- Paste the .env contents from
Dashboard > WPGraphQL > Settings > SnapWP Helperinto the file created.
Example environment variables from SnapWP Helper plugin screen. (Click for full screen)- Adjust any environment variables as needed.
- Save the file and close the editor.
- Paste the .env contents from
-
Return to the terminal and press
Enterto continue the setup process.
-
-
Start your headless WordPress app:
- Navigate to the newly created app.
- Run
npm run dev(for development) ornpm run build && npm run start(for production) - Visit the
NEXT_PUBLIC_FRONTEND_URLfrom.env(updated in Step 2), in your browser to see SnapWP in action!
-
Install the required packages in your Next.js project:
npm install --save @snapwp/blocks @snapwp/core @snapwp/next @snapwp/query @snapwp/plugin-apollo-client
-
Create an
.envfile in the project root. The contents for the .env file can be copied fromDashboard > WPGraphQL > Settings > SnapWP Helperfrom your WordPress backend. -
Create
snapwp.config.tsin the project root with the following content.// snapwp.config.ts import type { SnapWPConfig } from '@snapwp/core/config'; import { ApolloClientEngine } from '@snapwp/plugin-apollo-client'; const config: SnapWPConfig = { query: { engine: ApolloClientEngine, }, }; export default config;
-
Update your NextJS Config file.
Important: Make sure to use the
*.mjsfiletype for your config (i.enext.config.mjs) so that top-levelawaitcan be used.// next.config.mjs import withSnapWP from '@snapwp/next/withSnapWP'; export default await withSnapWP( {} );
-
Create a default app route at the path
src/app/[[...path]]/page.tsx. Use theTemplateRendererandEditorBlocksRendererto create the route.// src/app/[[...path]]/page.tsx import { TemplateRenderer } from '@snapwp/next'; import { EditorBlocksRenderer } from '@snapwp/blocks'; export default function Page() { return ( <TemplateRenderer> { ( editorBlocks ) => { return <EditorBlocksRenderer editorBlocks={ editorBlocks } />; } } </TemplateRenderer> ); }
Important: Make sure you have no other routes of the same specificity in your app. If you have an
app/page.tsxfile, you should delete it and instead integrate the code into thesrc/app/[[...path]]/page.tsxfile. -
Create the Root Layout to load Global Styles, Fonts and WordPress's enqueued scripts/styles for the route. Make a file
src/app/layout.tsx// src/app/layout.tsx import { RootLayout } from '@snapwp/next'; export default function Layout( { children }: React.PropsWithChildren<{}> ) { return ( <RootLayout> <>{ children }</> </RootLayout> ); }
Note
Currently, SnapWP only supports webpack for local development. If your existing project uses Turbopack, you will need to remove the --turbopack flag from the dev script in your package.json file.
...
"scripts": {
- "dev": "next dev --turbopack",
+ "dev": "next dev",
...
},
...@todo
If you encounter issues, check the GitHub Issues for known problems and workarounds.
This section contains a list of curated resources for developers working with headless WordPress, WPGraphQL, and some of the technologies used in the SnapWP stack.
- Docs: https://www.wpgraphql.com/docs/introduction
- Official Discord Community: https://wpgraphql.com/discord/
- Docs: https://nextjs.org/docs