Headless output

Hand a headless front end the same title, meta tags, canonical and JSON-LD that WordPress would print, through REST fields and a lookup route for any address.

4 min read
On this page
  1. Switch it on
  2. REST fields on posts and terms
  3. Look up any address
  4. The response
  5. Redirects
  6. Errors
  7. Privacy
  8. Filters
  9. From PHP
  10. With Hydrogen AI
  11. Related reading

If your pages are rendered outside WordPress, by Next.js, Astro, Nuxt or another front end that reads WordPress through the REST API, Hydrogen SEO can hand that front end the same head tags it prints on a normal WordPress page: the title, meta description, robots, canonical, Open Graph and Twitter tags, other named meta tags, and the JSON-LD graph. This is in version 0.0.36 and later.

Switch it on

Open Hydrogen SEO → General, find the Headless section and turn on Headless output, then save. It is off by default, and while it is off neither the REST fields nor the lookup route below exists. Leave it off if WordPress renders your pages.

The setting is stored as general.headless.enabled in the plugin's settings.

REST fields on posts and terms

Every public post type and public taxonomy that is shown in the REST API gets two read-only fields:

FieldTypeContents
hydrogen_seo_headstring or nullThe head tags as HTML, ready to put in <head>
hydrogen_seoobject or nullThe same values, parsed (shape below)
code
GET /wp-json/wp/v2/posts?slug=hello-world&_fields=id,title,hydrogen_seo
GET /wp-json/wp/v2/categories/5?_fields=hydrogen_seo_head

The values are worked out only when a field is part of the response. Leave both out with _fields and nothing is rendered; requests with context=embed leave them out too. When both fields are requested, the page is rendered once for the two.

A field is null for a post or term the person making the request could not open on the site. See privacy.

Look up any address

For pages that are not a single post or term, such as the homepage, archives, search and paginated lists, use the lookup route:

code
GET /wp-json/hydrogen-seo/v1/head?url=https://example.com/hello-world/
GET /wp-json/hydrogen-seo/v1/head?url=/category/news/page/2/

url is a full address on this site or a path starting with /. The route resolves:

  • posts, pages, attachments and custom post types
  • category, tag and custom taxonomy archives
  • author archives (for authors with published posts), post type archives and date archives
  • the homepage, including a static front page
  • search results (?s=)
  • plain permalinks such as ?p= and ?cat=
  • /page/N/ pagination

An address only matches a post, term or archive when that object's own link is the address requested. On sites without pretty permalinks the route is at /?rest_route=/hydrogen-seo/v1/head&url=....

The route needs no login. What it returns is limited to what the same visitor could open on the site.

The response

json
{
  "url": "https://example.com/hello-world/",
  "resolved": { "type": "post", "id": 12, "subtype": "post" },
  "redirect": null,
  "head": "<title>Hello world - Example</title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" />\n<link rel=\"canonical\" href=\"https://example.com/hello-world/\" />\n...",
  "seo": {
    "title": "Hello world - Example",
    "description": "A short summary.",
    "canonical": "https://example.com/hello-world/",
    "robots": "index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1",
    "open_graph": {
      "og:title": "Hello world - Example",
      "og:type": "article",
      "og:url": "https://example.com/hello-world/",
      "og:image": ["https://example.com/a.jpg", "https://example.com/b.jpg"]
    },
    "twitter": { "twitter:card": "summary_large_image", "twitter:title": "Hello world - Example" },
    "meta": { "google-site-verification": "..." },
    "schema": { "@context": "https://schema.org", "@graph": [] }
  }
}
  • resolved.type is one of post, term, author, date, post_type_archive, home or search.
  • A tag that appears more than once, such as og:image or article:tag, is a list.
  • meta holds the other named tags, such as webmaster verification codes and Googlebot-News.
  • schema is the JSON-LD graph, the same one the page prints, or null when the page prints no structured data.
  • The seo object is also what the hydrogen_seo REST field holds.

Redirects

When the address would redirect on the site, because an enabled rule in the redirection manager matches it or because it is an attachment page and attachment page redirects are on, the response says so instead of rendering a page:

json
{
  "url": "https://example.com/old-page/",
  "resolved": null,
  "redirect": { "to": "/new-page/", "status": 301 },
  "head": "",
  "seo": null
}

For a redirect rule, to is the target as it was entered in the rule, so it can be a path on your site or a full address. Send the visitor on with the status given.

Errors

StatusCodeWhen
400hydrogen_seo_invalid_urlThe address is not http or https, or its host or port is not the site's own
404hydrogen_seo_not_foundNothing lives at the address, or the person making the request could not open it

The route never fetches anything. An address on another host is refused before any lookup.

Privacy

Nothing is given out that the site would not show the same visitor.

  • Published posts of public post types: everyone.
  • Drafts, pending, scheduled and private posts: only users who may read that post, which for unpublished content means users who can edit it. Through the REST API that means a logged-in request with a wp_rest nonce, or an application password. Everyone else gets the same 404 as for a page that does not exist, and the REST fields are null.
  • Trash and auto-drafts, non-public post types and taxonomies: never.
  • Password-protected posts: the head is given out, as it is on the site, but the post's text is not used for the description or for the %excerpt% and %content% variables until the password has been entered. From 0.0.36 the same rule applies to the normal front end.
  • Author and date archives switched off in Titles & Meta: 404, as the site redirects them away.
  • noindex pages are given out with their robots value, as on the site.

Filters

  • hydrogen_seo_head_output receives ( array $result, array $query_args ): the head string and the seo array before they are returned.
  • hydrogen_seo_enable_frontend returning false empties the head output here too, as it does on the site.

See the filters reference for the rest.

From PHP

php
use HydrogenSEO\Inc\Modules\Headless\Output;

$out = Output::for_post( get_post( 12 ) ); // [ 'head' => string, 'seo' => array ]

Rendering points the main query at the object for the duration and calls the same code that prints the head on a normal page view, then puts the request's own query back. for_post() does not check visibility; that is the caller's job.

With Hydrogen AI

Hydrogen AI uses the same lookup to answer questions such as "why is the title of my pricing page wrong in Google". Its read works whether or not headless output is switched on, and because it acts as an administrator it can also see drafts and private pages.

Was this page helpful?
Still stuck? Contact support