GCBGutenberg
Control Blocks
Buy

Headless rendering

Rendering with a React component

Rendering with a React component instead of render.php

render.php is optional. If it's absent, the plugin renders the block by making a server-to-server request to a route on your Next.js (or Astro, Express, anything HTTP) frontend. That route returns HTML for the component; the plugin extracts it and hands it to the editor — same contract as render.php, no CORS, no React bundle ever loaded inside wp-admin.

This is the headless mode: author the block with React (shadcn, dnd-kit, anything), and treat WordPress as a CMS that holds typed fields.

The starter gcb-next-starter repo implements this contract end-to-end. Treat it as the reference; clone it and grow it into your real site, or copy from it into an existing Next.js project.

How to use it

  1. Create themes/{active-theme}/blocks/{block-name}/ with just a block.json and (optionally) a block.fields.json. Do not include render.php.

  2. Make sure WordPress can reach your frontend's /wordpress/render/[block] route. Defaults to http://localhost:3001. Override with either:

    • define('GCBLITE_COMPONENT_SERVER_URL', 'https://your-frontend.example.com'); in wp-config.php, or
    • add_filter('gcblite_frontend_url', fn() => 'https://...');.
  3. The frontend's /wordpress/render/[slug]?attrs={url-encoded-json} route must return HTML wrapped in <wp-block-wrapper data-block-name="{slug}" data-cache-timestamp="{ts}">…</wp-block-wrapper>. The wrapper element is the contract — the plugin's HTML extractor finds the markers and discards everything else (doctype, scripts, styles). data-cache-timestamp is optional but recommended; the plugin uses it to invalidate the per-attribute cache when your frontend restarts.

  4. The same React component renders both the editor preview and the public frontend. If you need different behaviour per context (e.g. lighter preview, heavier frontend with animation), export { admin, frontend } instead of a single default — the admin route picks the admin variant.

  5. Wire up the frontend's block registry so the slug after gcb/ maps to a React component. In the starter that's WP_BLOCK_REGISTRY['gcb/hero'] = Hero.

Mental model

Text
Editor in wp-admin              WordPress              Your Next.js frontend
─────────────────────           ────────────           ──────────────────────────
useBlockProps()
  └─ usePHPPreview()
        ↓ apiFetch
   POST /gcblite/v1/render-batch
        ↓
                          render_one()
                            ├─ render.php exists?
                            │    └─ run it locally
                            └─ otherwise:
                                 wp_remote_get
                                    ────────────→  GET /wordpress/render/{slug}
                                                            ↓
                                                       Renders React component
                                                       wrapped in <wp-block-wrapper>
                                    ←────────────  HTML
                                 HtmlExtractor::extract
                                 BlockWrapperParser::parse
        ↓ { html, wrapperAttributes }
   Editor injects HTML, applies wrapper attrs via useBlockProps

The same HTTP exchange happens for every attribute change (debounced into one batch per tick). Cache hits skip the wp_remote_get round-trip.

IMPORTANT: editor preview is static SSR — no client hydration

Your frontend renders the component once (server-side) and returns the HTML. WordPress injects that HTML into the editor as a string; React never hydrates in the editor. The public frontend hydrates as normal.

Concrete consequences when designing a block:

  1. Anything that requires JS to be visible will look broken in the editor. Examples:

    • Radix accordion items render closed; clicking the trigger does nothing.
    • useEffect never fires.
    • Hover/focus state managed by JS never updates.
    • Animations that start on mount never play.
  2. Components that read context from a Provider crash if rendered standalone. The editor calls each block's render endpoint independently — a child block is fetched on its own, not nested inside its parent. So if you use a library like Radix UI where Accordion.Item reads context from Accordion.Root, and you render only Accordion.Item, SSR returns 500. Fix: wrap the standalone component in its own provider (small overhead, nested providers in the public-frontend case are harmless).

  3. Use forceMount on collapsible Radix primitives. <RadixAccordion.Content forceMount> ensures children are in the DOM even when closed. Otherwise the editor preview has empty panels by definition.

  4. Editor-only CSS overrides live in /wordpress/editor.css. Your frontend publishes two stylesheets:

    • /wordpress/styles.css — frontend + editor canvas.
    • /wordpress/editor.css — editor only. For things like "force accordion open so the author can see the content", put the rule in editor.css (scoped under .editor-styles-wrapper).
  5. <Repeater /> and <InnerBlocks /> are marker tags, not actual rendering. When the editor receives <repeater> or <innerblocks> in the SSR HTML, gcb-lite's parse-preview.js swaps the marker for the real WP InnerBlocks UI (with Add button, drag-to-reorder, etc.). The marker mode is what surfaces the editor authoring affordances — your component should emit it when no blocks/children/html is passed.

If your block depends on JS to be useful (carousels, modals, complex interactions), the editor preview will show the static state only. That's usually fine for authoring; just don't fight it.

Theme blocks: the is-live convention

render.php blocks are in the same position: the canvas shows the server's HTML and runs none of the block's view.js. A scroll-driven or animated block (a pinned zoom, a scroll-stepped story, a reveal) therefore has two layouts, and the class is-live picks between them:

  • Without is-live — the storyboard. Everything visible at once, in reading order: every step stacked, every slide shown, nothing hidden behind scroll or a timer. This is what the canvas shows, what a reader without JS gets, and what an author edits in place.
  • With is-live — the experience. Only view.js adds the class, as its first act (el.classList.add('is-live')); all the pinning, hiding, transforms and timing are written under .is-live.
CSS
.my-story__step { margin-bottom: 2rem; }                        /* storyboard */
.my-story.is-live .my-story__step { position: absolute; opacity: 0; }
.my-story.is-live .my-story__step.is-active { opacity: 1; }

So the storyboard is the default and the experience is opt-in — the editor never needs JS to show the content, and a broken script still leaves a readable page. Editor-only help (step labels, pins drawn on the canvas) goes in .editor-styles-wrapper rules and the block's editor.js overlay (see "Drawing on a block in the editor").