Built on top of cms-wordpress example
robots.ts: This automatically gets the robots.txt of the API route and serves it on the/robots.txtroute.sitemap.ts: This automatically gets all paths from the API and generates a sitemap to serve on the/sitemap.xmlroute.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.js
pnpm dlx tiged getsieutoc/edo-tensei your-project
-
Set
Site Address (URL)to your frontend URL, e.g.https://localhost:3000in Settings -> General -
Make sure Permalinks are set to
Post namein Settings -> Permalinks -
Set
Sample pageasStatic pagein Settings -> Reading -
Create a new page called
404 not foundensuring the slug is404-not-found -
Install and activate following plugins:
- Add WPGraphQL SEO
- Classic Editor
- Redirection
- WPGraphQL
- WPGraphQL JWT Authentication
- Yoast SEO
- Advanced Custom Fields PRO (optional)
- WPGraphQL for ACF (optional)
-
Do first-time install of Redirection. Recommended to enable monitor of changes
-
Configure Yoast SEO with:
- Disable XML Sitemaps under Yoast SEO -> Settings
- If you did not change the
Site Address (URL)before installing Yoast, it will ask you to run optimize SEO data after changing permalinks, do so - Generate a robots.txt file under Yoast SEO -> Tools -> File Editor
- Modify robots.txt sitemap reference from
wp-sitemap.xmltositemap.xml
-
Enable Public Introspectionunder GraphQL -> Settings -
Add following constants to
wp-config.phpdefine('HEADLESS_SECRET', 'INSERT_RANDOM_SECRET_KEY'); define('HEADLESS_URL', 'INSERT_LOCAL_DEVELOPMENT_URL'); // http://localhost:3000 for local development define('GRAPHQL_JWT_AUTH_SECRET_KEY', 'INSERT_RANDOM_SECRET_KEY'); define('GRAPHQL_JWT_AUTH_CORS_ENABLE', true);
-
Create a bare minimum custom WordPress theme, consisting of only 2 files:
- style.css
- functions.php (see the bottom of this README)
- Clone the repository
- Run
npm installto install dependencies - Create
.envfile 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.tsxto match the post id of your "404 Not Found" page in WordPress -
npm run devand 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:
module.exports = {
client: {
service: {
name: "WordPress",
localSchemaFile: "./src/gql/schema.gql",
},
},
};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 recieving 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 application