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 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.
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:
- A Next.js project with the ForgeWP adapter running in decoupled mode (
FORGEWP_DEPLOYMENT_MODE=decoupledandFORGEWP_WP_URLset). See Next.js App Router Adapter. - The Companion Plugin active on that WordPress site. See The Companion Plugin.
- A
wpClientcreated withcreateWpServerClient(). Projects created withcreate-forgewpalready have one insrc/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():
1import type { Metadata } from 'next';
2import { toNextMetadata } from '@forgewp/react';
3import { wpClient } from '@/lib/content';
4
5type Props = { params: Promise<{ slug: string }> };
6
7export async function generateMetadata({ params }: Props): Promise<Metadata> {
8 const { slug } = await params;
9 const seo = await wpClient.getSeo(`/blog/${slug}/`);
10 return toNextMetadata(seo, { title: 'Blog' });
11}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.
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:
1import { WpJsonLd } from '@forgewp/react';
2import { wpClient } from '@/lib/content';
3
4export default async function PostPage({ params }: Props) {
5 const { slug } = await params;
6 const post = await wpClient.getPost('post', slug);
7 const seo = await wpClient.getSeo(`/blog/${slug}/`);
8
9 return (
10 <main>
11 <WpJsonLd schemas={seo?.jsonLd} />
12 <h1>{post?.title}</h1>
13 </main>
14 );
15}<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.
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:
1define( '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.
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.
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/:
1{
2 "path": "/en/contact/",
3 "lang": "en",
4 "provider": "yoast",
5 "objectType": "post",
6 "objectId": 17,
7 "title": "Contact — Example",
8 "description": "Get in touch with our team.",
9 "canonical": "https://www.example.com/en/contact/",
10 "robots": { "index": true, "follow": true, "maxImagePreview": "large" },
11 "openGraph": {
12 "title": "Contact — Example",
13 "description": "Get in touch with our team.",
14 "type": "article",
15 "url": "https://www.example.com/en/contact/",
16 "siteName": "Example",
17 "locale": "en_US",
18 "images": [{ "url": "https://cms.example.com/wp-content/uploads/contact.jpg", "width": 1200, "height": 630 }]
19 },
20 "twitter": { "card": "summary_large_image", "title": "…", "description": "…", "site": "@example", "creator": "", "images": ["…"] },
21 "alternates": {
22 "canonical": "https://www.example.com/en/contact/",
23 "languages": { "de": "https://www.example.com/kontakt/", "x-default": "https://www.example.com/kontakt/" }
24 },
25 "jsonLd": [{ "@context": "https://schema.org", "@graph": [] }]
26}Field Meaning providerWhich plugin wrote the metadata: yoast, rankmath, aioseo, seopress, or core when no SEO plugin is activeobjectTypeWhat 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-archivealternates.languagesThe 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, andgetSeo()returnsnull. - A full URL instead of a path answers
400. Pass/contact/, nothttps://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:
1'use client';
2import { useWpSeo } from '@forgewp/react';
3
4export function ShareButton() {
5 const { seo } = useWpSeo();
6 if (!seo) return null;
7 const shareUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(seo.canonical)}`;
8 return <a href={shareUrl}>Share</a>;
9}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 existsThe 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 to show editor-managed fields in the same app.