
This is an example on how you can build a Next.js 14 project (with App Router), using WordPress as the data source.
robots.ts: This automatically gets the robots.txt of the API route and serves it on the /robots.txt route.sitemap.ts: This automatically gets all paths from the API and generates a sitemap to serve on the /sitemap.xml route.middleware.ts: This contains a middleware function that checks the users path for stored redirects, and redirects the user if a match is found.[[...slug]]: This is the catch-all route that is used to render all pages. It is important that this route is not removed, as it is used to render all pages. It fetches the ContentType and renders the correspondingnot-found.tsx: This page is used for dynamic 404 handling - adjust the database id to match your decired WordPress page, and make sure the WordPress slug is "not-found", your 404 page will then be editable from your CMS.codegen.ts: Automatic type generation for your WordPress installationDraft Mode: Seamless Preview / Draft Preview support, using authentication through WPGraphQL JWT Authentication and Next.js Draft ModeOn Demand Cache Revalidation: Including a bare minimum WordPress theme that implements cache revalidation, WordPress link rewrites and other utils for integrating with Next.jsExecute create-next-app with npm, Yarn, pnpm, or Bun to bootstrap the example:
Deploy it to the cloud with Vercel (Documentation).
Set Site Address (URL) to your frontend URL, e.g. https://localhost:3000 in Settings -> General
Make sure Permalinks are set to Post name in Settings -> Permalinks
Set Sample page as Static page in Settings -> Reading
Create a new page called 404 not found ensuring the slug is 404-not-found
Install and activate following plugins:
Do first-time install of Redirection. Recommended to enable monitor of changes
Configure Yoast SEO with:
Site Address (URL) before installing Yoast, it will ask you to run optimize SEO data after changing permalinks, do sowp-sitemap.xml to sitemap.xmlEnable Public Introspection under GraphQL -> Settings
Add following constants to wp-config.php
Create a bare minimum custom WordPress theme, consisting of only 2 files:
npm install to install dependencies.env file in the root directory and add the following variables:| Name | Value | Example | Description | 
|---|---|---|---|
| NEXT_PUBLIC_BASE_URL | Insert base url of frontend | http://localhost:3000 | Used for generating sitemap, redirects etc. | 
| NEXT_PUBLIC_WORDPRESS_API_URL | Insert base url of your WordPress installation | http://wp-domain.com | Used when requesting wordpress for data | 
| NEXT_PUBLIC_WORDPRESS_API_HOSTNAME | The hostname without protocol for your WordPress installation | wp-domain.com | Used for dynamically populating the next.config images remotePatterns | 
| HEADLESS_SECRET | Insert the same random key, that you generated for your wp-config.php | INSERT_RANDOM_SECRET_KEY | Used for public exhanges between frontend and backend | 
| WP_USER | Insert a valid WordPress username | username | Username for a system user created specifically for interacting with your WordPress installation | 
| WP_APP_PASS | Insert application password | 1234 5678 abcd efgh | Generate an application password for the WordPress user defined in WP_USER | 
[!WARNING] >
WP_USERandWP_APP_PASSare critical for making preview and redirection work
Adjust the ID in not-found.tsx to match the post id of your "404 Not Found" page in WordPress
npm run dev and build an awesome application with WordPress!
[!NOTE] > Running
npm run devwill automatically generate typings from the WordPress installation found on the url provided in your environment variable:NEXT_PUBLIC_WORDPRESS_API_URL
We are generating typescript types from the provided schema with Codegen.
If you want to add auto completion for your queries, you can do this by installing the "Apollo GraphQL" extension in VS Code and adding an apollo.config.js file, next to the next.config.js, and add the following to it:
I will recommend building your page content by using the Flexible Content data type in ACF Pro. This will make you able to create a "Block Builder" editor experience, but still having everything automatically type generated, and receiving the data in a structured way. The default "Gutenberg" editor returns a lot of HTML, which makes you loose a lot of the advantages of using GraphQL with type generation.
The example supports the WordPress "Redirection" plugin. the WP_USER and WP_APP_PASS environment variables are required, for this to work. By implementing this you can manage redirects for your content, through your WordPress CMS
The example supports WordPress preview (also draft preview), when enabling draftMode in the api/preview/route.ts it logs the WP_USER in with the WP_APP_PASS and requests the GraphQL as an authenticated user. This makes draft and preview available. If a post is in "draft" status, it doesn't have a real slug. In this case we redirect to a "fake" route called /preview/${id} and uses the supplied id for fetching data for the post.
All our GraphQL requests has the cache tag wordpress - when we update anything in WordPress, we call our /api/revalidate route, and revalidates the wordpress tag. In this way we ensure that everything is up to date, but only revalidate the cache when there actually are updates.
We use an "Optional Catch-all Segment" for handling all WordPress content. When rendering this component we simply ask GraphQL "what type of content is this route?" and fetch the corresponding template. Each template can then have their own queries for fetching specific content for that template.
We are using Yoast SEO for handling SEO in WordPress, and then all routes are requesting the Yoast SEO object, and parsing this to a dynamic generateMetadata() function
The boilerplate is structured as follows:
app: Contains the routes and pages of the applicationassets: Contains helpful styles such as the variablescomponents: Contains the components used in the applicationgql: Contains auto-generated types from GraphQL via CodeGenqueries: Contains reusable data fetch requests to GraphQLutils: Contains helpful functions used across the applicationThis functions.php is implementing different useful features for using WordPress with Next.js:
Navigation..tsx)