# SEO Metadata in Decoupled Mode


> Show the same page titles, descriptions, social previews and search-engine data in a decoupled Next.js app as on the WordPress site, using getSeo(), toNextMetadata() and <WpJsonLd>.


**SEO metadata** is information about a page that visitors mostly do not see on the page itself, but that search engines and social networks read. It lives in the page's `<head>` section. For example:

| Piece of metadata | What it does | Where you notice it |
| --- | --- | --- |
| **Title** | The page's name | The browser tab and the blue link in Google results |
| **Description** | A one or two sentence summary | The grey text under the link in Google results |
| **Canonical URL** | The one official address of the page, so search engines do not count copies twice | Not visible; read by search engines |
| **Robots** | Whether search engines may list the page and follow its links | Not visible; read by search engines |
| **Social preview** (Open Graph and Twitter card) | The title, text and image shown when someone shares the link | Link previews in WhatsApp, LinkedIn, X, Slack |
| **Language alternates** (hreflang) | The addresses of the same page in other languages | Google shows visitors the version in their language |
| **Structured data** (JSON-LD) | Facts about the page written for machines, such as "this is an article by Ada, published on 1 November" | Rich results in Google, such as article dates or breadcrumbs |

Most WordPress sites let an SEO plugin such as **Yoast SEO** or **Rank Math** write all of this, and editors adjust it in the WordPress admin.

## Why decoupled mode needs this page

In a **native** export, WordPress builds every page, so your SEO plugin's metadata is already in each page's `<head>`. You do not need to do anything.

In **decoupled** mode, your Next.js app builds the pages and only asks WordPress for content (see [Next.js App Router Adapter](https://www.forgewp.dev/docs/adapters/nextjs/nextjs-adapter) for the two modes). WordPress never builds the page, so the SEO plugin never gets the chance to add its metadata. Without the steps below, your decoupled pages would have no description, no social preview and no structured data.

ForgeWP fixes this with the **`seo` resource**. Your app asks WordPress, "what metadata would you print for this page?", and WordPress answers with exactly what it would have printed. Your app then adds it to its own page.

**Flowchart**

```mermaid
flowchart LR
  A[Visitor opens /blog/hello/] --> B[Your Next.js app]
  B -->|getSeo of /blog/hello/| C[WordPress with the Companion Plugin]
  C --> D[SEO plugin builds the metadata, as on a native page]
  D -->|title, description, social preview, ...| B
  B --> E[Page with the same head as the WordPress site]
```

> [!NOTE]
> **An SEO plugin is optional**
> Yoast SEO, Rank Math, All in One SEO and SEOPress are detected automatically. Without any of them, ForgeWP uses WordPress's own page title and takes the description from the post excerpt or, if there is none, from the site tagline (**Settings → General**).

## Before you start

You need:

1. A Next.js project with the ForgeWP adapter running in decoupled mode (`FORGEWP_DEPLOYMENT_MODE=decoupled` and `FORGEWP_WP_URL` set). See [Next.js App Router Adapter](https://www.forgewp.dev/docs/adapters/nextjs/nextjs-adapter).
2. The **Companion Plugin** active on that WordPress site. See [The Companion Plugin](https://www.forgewp.dev/docs/core/export-deployment/companion-plugin).
3. A `wpClient` created with `createWpServerClient()`. Projects created with `create-forgewp` already have one in `src/lib/content.ts`.

## Step 1: Add metadata to a page

Next.js reads a page's metadata from a function called `generateMetadata()` that you export from the page file. Ask WordPress for the page's SEO data and convert it with `toNextMetadata()`:

**src/app/blog/[slug]/page.tsx**

```tsx
import type { Metadata } from 'next';
import { toNextMetadata } from '@forgewp/react';
import { wpClient } from '@/lib/content';

type Props = { params: Promise<{ slug: string }> };

export async function generateMetadata({ params }: Props): Promise<Metadata> {
  const { slug } = await params;
  const seo = await wpClient.getSeo(`/blog/${slug}/`);
  return toNextMetadata(seo, { title: 'Blog' });
}
```

What each part does:

- `wpClient.getSeo('/blog/hello/')` asks WordPress for the metadata of the page at that address. Pass the **path** visitors see in the address bar, starting with `/`, not a full URL.
- `toNextMetadata(seo, fallback)` turns WordPress's answer into the object Next.js expects: title, description, robots, social preview and alternates.
- The second argument, `{ title: 'Blog' }`, is a **fallback**. It is used for anything WordPress does not provide, and for the whole page when WordPress has no published page at that path.

Open the page in your browser, right-click and choose **View Page Source**. The `<head>` now contains the same `<title>`, `<meta name="description">` and `og:` tags as the page on your WordPress site.

> [!TIP]
> **Paths must match the WordPress address**
> Use the same path WordPress uses for the page, including a language prefix such as `/en/` and the trailing slash if your WordPress permalinks have one. If `getSeo()` returns `null` for a page you know exists, compare the path with the page's address in WordPress.

## Step 2: Add structured data

Next.js metadata has no field for structured data, so ForgeWP gives you a small component that prints it into the page:

**src/app/blog/[slug]/page.tsx**

```tsx
import { WpJsonLd } from '@forgewp/react';
import { wpClient } from '@/lib/content';

export default async function PostPage({ params }: Props) {
  const { slug } = await params;
  const post = await wpClient.getPost('post', slug);
  const seo = await wpClient.getSeo(`/blog/${slug}/`);

  return (
    <main>
      <WpJsonLd schemas={seo?.jsonLd} />
      <h1>{post?.title}</h1>
    </main>
  );
}
```

`<WpJsonLd>` prints each item as a `<script type="application/ld+json">` tag, escaped so the data cannot break the page. It prints nothing when there is no structured data.

> [!NOTE]
> **Calling getSeo() twice is fine**
> `generateMetadata()` and the page both call `getSeo()` for the same path. Next.js reuses the first response during the same request, so WordPress is asked only once.

## Step 3: Use your public domain in the official URLs

A decoupled site usually has two addresses: WordPress runs on one (for example `cms.example.com`) and visitors use another (for example `www.example.com`). The canonical URL, the language alternates and the structured data must point at the **address visitors use**. Otherwise search engines send people to your WordPress server.

Tell WordPress your public address in `wp-config.php`:

**wp-config.php**

```php
define( 'FORGEWP_FRONTEND_ORIGIN', 'https://www.example.com' );
```

From then on, page addresses in the `seo` resource use `https://www.example.com`. Image addresses do **not** change, because the image files really are on your WordPress server or CDN.

> [!IMPORTANT]
> **One setting, two effects**
> `FORGEWP_FRONTEND_ORIGIN` also tells WordPress which other site may use a visitor's login session. If you list several addresses separated by commas, the first one is used for official page URLs. See [Auth deployment](https://www.forgewp.dev/docs/auth/auth-deployment/deployment).

## What WordPress sends back

You rarely need to read this directly, but it helps when something looks wrong. You can open it in your browser at `https://cms.example.com/wp-json/forgewp/v1/seo?path=/en/contact/`:

**GET /wp-json/forgewp/v1/seo?path=/en/contact/**

```json
{
  "path": "/en/contact/",
  "lang": "en",
  "provider": "yoast",
  "objectType": "post",
  "objectId": 17,
  "title": "Contact — Example",
  "description": "Get in touch with our team.",
  "canonical": "https://www.example.com/en/contact/",
  "robots": { "index": true, "follow": true, "maxImagePreview": "large" },
  "openGraph": {
    "title": "Contact — Example",
    "description": "Get in touch with our team.",
    "type": "article",
    "url": "https://www.example.com/en/contact/",
    "siteName": "Example",
    "locale": "en_US",
    "images": [{ "url": "https://cms.example.com/wp-content/uploads/contact.jpg", "width": 1200, "height": 630 }]
  },
  "twitter": { "card": "summary_large_image", "title": "…", "description": "…", "site": "@example", "creator": "", "images": ["…"] },
  "alternates": {
    "canonical": "https://www.example.com/en/contact/",
    "languages": { "de": "https://www.example.com/kontakt/", "x-default": "https://www.example.com/kontakt/" }
  },
  "jsonLd": [{ "@context": "https://schema.org", "@graph": [] }]
}
```

| Field | Meaning |
| --- | --- |
| `provider` | Which plugin wrote the metadata: `yoast`, `rankmath`, `aioseo`, `seopress`, or `core` when no SEO plugin is active |
| `objectType` | What the path points at: `front-page`, `posts-page` (your blog page), `post` (also pages and custom post types), `term` (a category or tag archive) or `post-type-archive` |
| `alternates.languages` | The page in other languages. Filled when Polylang or WPML is active; empty (`{}`) on a single-language site |

## What is never returned

- **Unpublished content.** Drafts, private posts and addresses that do not exist answer `404`, and `getSeo()` returns `null`.
- **A full URL** instead of a path answers `400`. Pass `/contact/`, not `https://example.com/contact/`.
- **Password-protected posts** return their title, official URL, alternates and robots, but an empty description, empty social descriptions and no structured data, because SEO plugins create those from the protected text.

## Read the metadata in a client component

On a native page and in client components you can also read the same data with the `useWpSeo()` hook, for example for a share button:

**src/components/ShareButton.tsx**

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

export function ShareButton() {
  const { seo } = useWpSeo();
  if (!seo) return null;
  const shareUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(seo.canonical)}`;
  return <a href={shareUrl}>Share</a>;
}
```

`useWpSeo()` uses the current page's address. On a native page the data is already included in the page; in a decoupled app the hook asks WordPress for it.

## Troubleshooting

| What you see | What to check |
| --- | --- |
| `getSeo()` returns `null` for a page that exists | The page must be published, and the path must match its WordPress address exactly, including a language prefix and the trailing slash. |
| The official URL points at the WordPress server | Add `FORGEWP_FRONTEND_ORIGIN` to `wp-config.php` (Step 3). |
| The title is only the site name | Without an SEO plugin, WordPress builds the title as "Post title – Site name". Set a title in your SEO plugin, or pass a fallback to `toNextMetadata()`. |
| Plugin data is missing | Open the same page on the WordPress site and view its source. The `seo` resource reports exactly what WordPress prints there. If the tags are missing there too, check the SEO plugin's settings. |

Continue with [Editable Fields in Decoupled Mode](https://www.forgewp.dev/docs/core/content-ownership/editables-in-decoupled-mode) to show editor-managed fields in the same app.

---

Source: [SEO Metadata in Decoupled Mode](https://www.forgewp.dev/docs/adapters/nextjs/seo-in-decoupled-mode)
