Editable Fields in Decoupled Mode
Show editor-managed fields from defineEditable() schemas, including repeaters and images, in a decoupled Next.js app with getEditable() and useWpEditables().
Page Content Slots and Editable Fields shows how to give editors named fields, such as a hero heading or a list of team members, with a defineEditable() schema. This page shows how a decoupled Next.js app reads those same fields.
Why decoupled mode needs this page
When you write a schema, ForgeWP registers each field in WordPress. Editors fill the fields in the WordPress admin, and WordPress saves the values with the page.
In a native export, the generated theme reads the saved values while WordPress builds the page. You do not need anything from this page.
In decoupled mode, your Next.js app builds the pages (see Next.js App Router Adapter). It needs a way to ask WordPress for the saved values. ForgeWP provides that as the editables resource: your app names a page, and WordPress answers with that page's field values, already cleaned up and in a predictable format.
Only declared fields are ever shared
WordPress only returns fields that are listed in a schema file under cms/editables/. Anything else saved with the page stays private, and so do fields you mark private: true.
Before you start
You need:
- A schema file in
cms/editables/, for example created withpnpm forgewp make:editable AboutPage. See Create a page editable schema. - A Next.js project with the ForgeWP adapter running in decoupled mode (
FORGEWP_DEPLOYMENT_MODE=decoupledandFORGEWP_WP_URLset). - The Companion Plugin, exported after you created or changed the schema, active on that WordPress site. See The Companion Plugin.
Re-export after changing a schema
WordPress learns which fields exist from your export. When you add or rename a field, run pnpm forgewp export and update the Companion Plugin, or the new field will not be returned.
Step 1: Find the page's route ID
Your app tells WordPress which page it wants. The most reliable way is the page's route ID: a name ForgeWP gives each page template, which does not change when an editor renames the page or its address.
Where the page comes from Route ID Vite project: src/app/pages/AboutPage.tsxabout-pageNext.js project: src/app/about/page.tsxaboutYour site's front page front-page |
|---|
To see every route ID on your site, open https://your-wordpress-site/wp-json/forgewp/v1/routes in the browser and look at the routeId values.
The schema file for a page uses the same name, for example cms/editables/about-page.ts for the about-page route. That is how WordPress knows which fields belong to which page.
Step 2: Read the fields on the server
In a page file, ask for the fields with wpClient.getEditable():
1import { wpClient } from '@/lib/content';
2import { editable } from '../../../cms/editables/about';
3
4export default async function AboutPage() {
5 const fields = await wpClient.getEditable(editable, { routeId: 'about' });
6
7 return (
8 <main>
9 <h1>{fields.hero_title}</h1>
10 <div dangerouslySetInnerHTML={{ __html: fields.hero_subtitle }} />
11 </main>
12 );
13}What each part does:
editableis your schema.getEditable()returns one value for every field in it.{ routeId: 'about' }says which page to read.- For each field you get the value an editor saved, or the field's
defaultfrom the schema when nothing is saved yet. Your page always has something to show.
hero_subtitle is a richText field, so it contains HTML such as <p>…</p>. Render it with dangerouslySetInnerHTML, as above. WordPress removes unsafe markup such as <script> before sending it.
The same code works in a native export
When you export a native theme, ForgeWP turns getEditable() into code that reads each field from WordPress directly. The one exception is repeater fields: in a native export getEditable() returns their default rows. For repeaters that must show saved rows in a native theme, use <WpRepeater name="…"> as shown in Page Content Slots and Editable Fields.
Other ways to name the page
If you do not have a route ID, use one of these instead:
Option Example Use it when routeId{ routeId: 'about' }The page belongs to one of your app's routes. Recommended. path{ path: '/en/about/' }You only have the page's address. Include the language prefix, if any. post{ post: 42 }You already know the WordPress post ID. |
|---|
You can also pass lang: 'de' for a specific language on a multilingual site (Polylang or WPML).
Step 3: Read the fields in a client component
Components that run in the browser (files starting with 'use client') cannot call wpClient. Use the useWpEditables() hook instead. It returns the same field values:
1'use client';
2import { useWpEditables } from '@forgewp/react';
3
4type Member = { name: string; role: string };
5
6export function TeamList() {
7 const { fields, loading } = useWpEditables({ routeId: 'about' });
8 const team = (fields.team as Member[] | undefined) ?? [];
9
10 if (loading) return null;
11
12 return (
13 <ul>
14 {team.map((member) => (
15 <li key={member.name}>
16 {member.name}, {member.role}
17 </li>
18 ))}
19 </ul>
20 );
21}useWpEditables() asks WordPress once and re-renders when the answer arrives. loading is true until then. If the page you are on already includes the data (native pages do), the hook uses it and makes no request.
useWpMeta() and WpRepeater in decoupled apps
useWpMeta() and <WpRepeater> read the same data. In a native export the page already includes it. In a decoupled app they show the saved values once the page's fields have been loaded with useWpEditables(); until then they show their defaults. So call useWpEditables() once on the page, for example in the component that contains them.
What each field type looks like
WordPress converts every value to the type you declared, so your components always receive the same kind of value:
Field type in the schema What your component receives When the editor saved nothing text, url, color, select, iconA string The field's default, or ""richTextAn HTML string with paragraphs, unsafe markup removed The default as you wrote it, or ""numberA number, for example 1999 (not the text "1999")The default, or 0booleantrue or falseThe default, or falseimageAn object: { id, url, alt, width, height }The default, or nullrepeaterA list of rows; each value inside a row follows these same rules The default rows, or an empty list [] |
|---|
Images are objects, not addresses
An image field gives you { url: 'https://…/photo.jpg', alt: 'Ada at work', … }, also inside repeater rows. Use image.url for the address and image.alt for the alternative text. Check for null before using it, because the field may be empty.
1{fields.hero_image ? (
2 <img src={fields.hero_image.url} alt={fields.hero_image.alt} />
3) : null}What is never returned
- Unpublished pages. Drafts and private pages are never returned.
getEditables()returnsnullandgetEditable()returns your defaults. - Password-protected pages return no field values, so your components show their defaults until a visitor unlocks the page on the WordPress site.
- Undeclared and private fields, as explained at the top of this page.
What WordPress sends back
You rarely need the raw answer, but it helps when a value looks wrong. Open it in the browser at https://your-wordpress-site/wp-json/forgewp/v1/editables?routeId=about:
1{
2 "postId": 15,
3 "postType": "page",
4 "routeId": "about",
5 "path": "/about/",
6 "lang": "en",
7 "schema": "about",
8 "protected": false,
9 "fields": {
10 "hero_title": "About our studio",
11 "hero_subtitle": "<p>We build sites that run natively and decoupled.</p>\n",
12 "founded": 1999,
13 "hero_image": {
14 "id": 88,
15 "url": "https://your-wordpress-site/wp-content/uploads/studio.jpg",
16 "alt": "Our studio",
17 "width": 1600,
18 "height": 900
19 },
20 "team": [{ "name": "Ada", "role": "Engineering" }]
21 }
22}To get this whole object in your app instead of only the field values, use wpClient.getEditables({ routeId: 'about' }).
Schemas in Next.js server files
In files that run on the server in Next.js (page files and other files without 'use client'), import { defineEditable, text } from '@forgewp/react' currently fails with "Export defineEditable doesn't exist". Until that is fixed, write the schema as a plain object. ForgeWP reads both forms the same way:
1export const editable = {
2 hero_title: { type: 'text', label: 'Hero Title', default: 'Edit me' },
3 hero_subtitle: { type: 'richText', label: 'Hero Subtitle', default: 'Edit me' },
4 team: {
5 type: 'repeater',
6 label: 'Team',
7 fields: {
8 name: { type: 'text', label: 'Name', default: '' },
9 role: { type: 'text', label: 'Role', default: '' },
10 },
11 default: [],
12 },
13} as const;Each field is an object with the same type, label and default you would pass to text(), richText() and the other helpers.
Troubleshooting
What you see What to check The decoupled page shows the defaults, the WordPress page shows saved values Open the raw answer (see above). If schema is null, the schema file name does not match the page's route ID: rename the file, or pass schema: 'your-file-name'. Then re-export and update the Companion Plugin.A new field is missing Re-export and update the Companion Plugin after changing the schema. getEditables() returns nullThe page must be published. When using path, it must match the page's address in WordPress, including any language prefix.Code that expected an image address breaks Image fields are objects now. Use .url. |
|---|
Continue with SEO Metadata in Decoupled Mode to bring the page's search and social metadata into the same app.