WordPress

WordPress REST API + JSON-LD: One Source of Truth for Structured Data

The WordPress REST API and JSON-LD work best when they share one source of truth: register structured fields with register_post_meta, expose them through register_rest_field, then serialize the same values into JSON-LD at render time. In my builds at webdev-saif.com in Mymensingh, that single model keeps visible content, API responses and schema in sync, so answer engines quote the same facts a reader sees.

By Saif Al Jahanwebdev-saif.com

Why one source of truth matters

Most WordPress sites treat structured data as a separate job from content. An SEO plugin writes one set of fields, a theme renders another, and a headless front end reads a third through the REST API. The result is drift: the page says one price, the schema says another, and an answer engine has no reason to trust either. I avoid that by defining every structured value once, in the PHP data layer, and letting three consumers read it: the rendered template, the REST response, and the JSON-LD graph. There is no duplicate copy to maintain and no sync cron to debug. One change to a field updates what a visitor reads, what an API client receives, and what a crawler is told in the same deployment.

  • One PHP data layer as the canonical source for every structured field
  • The theme template, REST endpoint and JSON-LD graph all read that layer
  • No duplicated values in post meta, plugin settings and template hard-coding
  • A single deployment updates visible copy, API output and schema together

Register the structured fields once

Start with register_post_meta rather than a custom SQL table or loose options entries. Each field gets an explicit type, a single flag, a sanitize_callback and an auth_callback, and, crucially, show_in_rest set to true. That last argument is what puts the value into the standard /wp-json/wp/v2/<post-type> response without writing custom endpoint code. I keep the field definitions in one PHP class so the same array drives registration, default values and the schema builder. Use date strings for dates, decimal strings for prices where precision matters, and enum-style values for statuses instead of free text. A small, typed field set beats a single JSON blob in post meta: it is queryable with meta_query, validated on save, and trivial to map into a schema.org property later.

  • register_post_meta with show_in_rest => true for every structured property
  • Explicit type, single => true and sanitize_callback on each field
  • auth_callback to restrict writes to the right WordPress capability
  • Typed values instead of one opaque JSON string in post meta
  • One PHP class that owns defaults, registration and schema mapping

Expose the fields through the REST API

register_post_meta covers the standard endpoints, but a public API needs a deliberately shaped response. I use register_rest_field to add a namespace key such as seo_schema to posts, pages and custom post types, with a get_callback that returns only fields intended for public consumption. The update_callback stays absent for public data so the endpoint is read-only; where edits are needed, a permission_callback checks current_user_can before anything is written. The third callback declares the response schema, which makes the field discoverable to API clients and to documentation tools. Deciding what not to expose matters as much as what you expose: internal IDs, draft notes and cached HTML do not belong in a public response, even if they live in the same row.

  • register_rest_field with a stable namespace such as seo_schema
  • get_callback returns a whitelist of public fields, never the raw meta row
  • permission_callback and current_user_can on any write path
  • A schema callback so clients can discover types and required properties

Emit JSON-LD from the same values

The output layer should read the same field definitions, not reconstruct them from rendered HTML. I build a single @graph in PHP and print it on wp_head with wp_json_encode using JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE, wrapped in a script tag with type application/ld+json. The graph includes the WebPage or TechArticle node, a FAQPage node built from the same question list the template renders, and BreadcrumbList entries derived from the actual hierarchy. Properties such as headline, datePublished, dateModified and author map directly to registered fields. Because the graph is generated server-side at render time, there is no client-side hydration delay and no chance of a tag manager firing markup that disagrees with the page. Validate the encoded output, not the PHP array: escaping bugs show up only in the final JSON.

  • One @graph containing WebPage, TechArticle, FAQPage and BreadcrumbList nodes
  • wp_json_encode on wp_head, never injected after load by a tag manager
  • dateModified taken from the same field the template displays
  • FAQ questions generated from the rendered list, not a separate copy
  • Validate the emitted JSON string, not just the PHP array

Keep visible content and schema aligned

Alignment is a discipline, not a feature. If the hero shows a service price, that number must come from the same meta field that feeds offers.price in the graph. If the byline says Saif Al Jahan, the author node must resolve to the same Person URL rather than a free-text name typed twice. I also keep one canonical URL function and call it from the rel=canonical tag, the WebPage @id and the share tags. Page builders are the usual source of drift: their wrappers split headings from the content they describe, so schema generated from DOM scraping often captures the wrong node. Rendering the graph from the data layer sidesteps that entirely. For headless setups, the same REST response that feeds the front end feeds the schema builder, so a decoupled UI cannot invent facts the API does not have.

Verify, cache and maintain the output

Verification is fast when the structure is predictable. Fetch the REST endpoint with curl and confirm every public field is present with the expected type. Then fetch the rendered page and extract the JSON-LD block, paste it into a schema validator and check that required properties match visible content. I keep a small PHPUnit test that asserts the graph and the REST response serialize the same values for a fixture post; it catches drift the moment someone adds a duplicate field. On caching, invalidate the page cache when a structured field changes, not just when post content changes, or the schema will lag behind the API. Review the graph after every theme or plugin update, because hook priorities on wp_head are a common cause of duplicated or missing script tags.

  • curl /wp-json/wp/v2/posts?slug=... to inspect the public field set
  • Validate emitted JSON-LD against Schema.org and Rich Results guidance
  • A PHPUnit assertion that graph values equal REST values for a fixture
  • Page-cache invalidation tied to structured-field updates, not only saves
  • Check wp_head hook priority after theme or plugin updates

Frequently asked questions

What does combining the WordPress REST API with JSON-LD actually give me?

One canonical set of facts. Register structured fields once in PHP, expose them through the WordPress REST API, and serialize those same values into JSON-LD at render time. A reader, an API client and an answer engine then see identical titles, dates, authors and FAQ answers. The win is maintenance: one edit updates all three surfaces, instead of three plugins that slowly disagree.

Do I need a plugin to output JSON-LD from WordPress?

No. WordPress core already gives you register_post_meta and register_rest_field, and wp_json_encode handles the output. A small custom plugin or theme class is enough to register fields, expose them and print one JSON-LD graph on wp_head. General-purpose SEO plugins can do part of this, but their field UI becomes another source of truth you have to keep in sync with your templates.

Should schema be generated on the server or in JavaScript?

Server-side, every time. Generate the graph in PHP during the request and print it in the head. JSON-LD assembled in JavaScript after hydration may be missed by crawlers that do not execute scripts, and it can lag behind the visible content by a render cycle. Server-side output is present in the first HTML response, which is what search and answer engines fetch.

Which fields should I expose in the REST API?

Only fields you are willing to publish and defend. Typical examples are headline, description, author, datePublished, dateModified, canonical URL, FAQ entries and any product or service properties shown on the page. Keep internal IDs, draft notes and cached HTML out of the response. Use a get_callback that whitelists fields explicitly, and an auth_callback on registration so edits cannot come from an unauthenticated request.

How do I stop the schema and the page from drifting apart?

Make both surfaces read one data layer. Build the graph from the same registered fields the template uses, and derive FAQ nodes from the same question array the page renders. Then add a test that compares the JSON-LD values with the REST response for a fixture post. In my builds in Mymensingh that single assertion catches more drift than any manual audit.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .