# 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](https://www.forgewp.dev/docs/core/content-ownership/page-content-and-editables) 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](https://www.forgewp.dev/docs/adapters/nextjs/nextjs-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.

**Flowchart**

```mermaid
flowchart LR
  A[Editor fills fields in wp-admin] --> B[(WordPress saves the values)]
  B --> C[Native theme reads them while building the page]
  B --> D[editables resource]
  D -->|getEditable / useWpEditables| E[Your decoupled Next.js app]
```

> [!NOTE]
> **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:

1. A schema file in `cms/editables/`, for example created with `pnpm forgewp make:editable AboutPage`. See [Create a page editable schema](https://www.forgewp.dev/docs/core/content-ownership/page-content-and-editables).
2. A Next.js project with the ForgeWP adapter running in decoupled mode (`FORGEWP_DEPLOYMENT_MODE=decoupled` and `FORGEWP_WP_URL` set).
3. The **Companion Plugin**, exported **after** you created or changed the schema, active on that WordPress site. See [The Companion Plugin](https://www.forgewp.dev/docs/core/export-deployment/companion-plugin).

> [!IMPORTANT]
> **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.tsx` | `about-page` |
| Next.js project: `src/app/about/page.tsx` | `about` |
| Your 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()`:

**src/app/about/page.tsx**

```tsx
import { wpClient } from '@/lib/content';
import { editable } from '../../../cms/editables/about';

export default async function AboutPage() {
  const fields = await wpClient.getEditable(editable, { routeId: 'about' });

  return (
    <main>
      <h1>{fields.hero_title}</h1>
      <div dangerouslySetInnerHTML={{ __html: fields.hero_subtitle }} />
    </main>
  );
}
```

What each part does:

- `editable` is 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 `default` from 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.

> [!TIP]
> **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](https://www.forgewp.dev/docs/core/content-ownership/page-content-and-editables).

### 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:

**src/components/TeamList.tsx**

```tsx
'use client';
import { useWpEditables } from '@forgewp/react';

type Member = { name: string; role: string };

export function TeamList() {
  const { fields, loading } = useWpEditables({ routeId: 'about' });
  const team = (fields.team as Member[] | undefined) ?? [];

  if (loading) return null;

  return (
    <ul>
      {team.map((member) => (
        <li key={member.name}>
          {member.name}, {member.role}
        </li>
      ))}
    </ul>
  );
}
```

`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.

> [!NOTE]
> **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`, `icon` | A string | The field's `default`, or `""` |
| `richText` | An HTML string with paragraphs, unsafe markup removed | The `default` as you wrote it, or `""` |
| `number` | A number, for example `1999` (not the text `"1999"`) | The `default`, or `0` |
| `boolean` | `true` or `false` | The `default`, or `false` |
| `image` | An object: `{ id, url, alt, width, height }` | The `default`, or `null` |
| `repeater` | A list of rows; each value inside a row follows these same rules | The `default` rows, or an empty list `[]` |

> [!WARNING]
> **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.

**Using an image field**

```tsx
{fields.hero_image ? (
  <img src={fields.hero_image.url} alt={fields.hero_image.alt} />
) : null}
```

## What is never returned

- **Unpublished pages.** Drafts and private pages are never returned. `getEditables()` returns `null` and `getEditable()` 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`:

**GET /wp-json/forgewp/v1/editables?routeId=about**

```json
{
  "postId": 15,
  "postType": "page",
  "routeId": "about",
  "path": "/about/",
  "lang": "en",
  "schema": "about",
  "protected": false,
  "fields": {
    "hero_title": "About our studio",
    "hero_subtitle": "<p>We build sites that run natively and decoupled.</p>\n",
    "founded": 1999,
    "hero_image": {
      "id": 88,
      "url": "https://your-wordpress-site/wp-content/uploads/studio.jpg",
      "alt": "Our studio",
      "width": 1600,
      "height": 900
    },
    "team": [{ "name": "Ada", "role": "Engineering" }]
  }
}
```

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:

**cms/editables/about.ts**

```ts
export const editable = {
  hero_title: { type: 'text', label: 'Hero Title', default: 'Edit me' },
  hero_subtitle: { type: 'richText', label: 'Hero Subtitle', default: 'Edit me' },
  team: {
    type: 'repeater',
    label: 'Team',
    fields: {
      name: { type: 'text', label: 'Name', default: '' },
      role: { type: 'text', label: 'Role', default: '' },
    },
    default: [],
  },
} 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 `null` | The 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](https://www.forgewp.dev/docs/adapters/nextjs/seo-in-decoupled-mode) to bring the page's search and social metadata into the same app.

---

Source: [Editable Fields in Decoupled Mode](https://www.forgewp.dev/docs/core/content-ownership/editables-in-decoupled-mode)
