GCBGutenberg
Control Blocks
v1.3.3
Buy

How to set up headless WordPress with visual editing in Next.js

A guide to headless WordPress development where the block editor loads each block preview from your Next.js front end, so your client edits the real design.

Going headless usually costs you the editor. WordPress holds the content, Next.js renders the site, and your client edits grey boxes and forms because the block editor can't show components that live in another app. Gutenberg Control Blocks (GCB) fixes that. When a block has no PHP template, WordPress asks your front end for the block's HTML and shows it in the editor. Your client edits the real design, and the same component renders the public page.

This guide sets that up with Next.js. Any framework that can render HTML on the server works the same way, because the contract between WordPress and your front end is plain HTML.

How it fits together

  1. Your client edits a page in the block editor. The fields save as ordinary block attributes.
  2. For each block, the editor asks WordPress for a preview. A block with a render.php renders in PHP, as usual. A block without one is sent to your front end.
  3. WordPress calls GET {your front end}/wordpress/render/{slug}?attrs=…, server to server. Your route renders the component and returns its HTML.
  4. The editor shows that HTML. For the public site, your front end reads the saved blocks from the REST API and renders the same components.

There's no React bundle in wp-admin and no CORS to set up. Previews for every block on the page go in one batched request, and repeat requests are cached.

1. Make the block without a template

A headless block is a folder in your theme with block.json and block.fields.json, and no render.php. The block is named after its folder, so blocks/solace-badge/ is gcb/solace-badge.

block.fields.json
{  "controls": [    { "id": "c_label", "type": "text", "label": "Label",      "attributeKey": "label", "default": "New in" }  ]}

You can mix both kinds on one site. Keep render.php for blocks that are fine in PHP, and leave it out for the ones your front end renders.

2. Point WordPress at your front end

Nothing is contacted until you set an address. Set it in wp-config.php:

wp-config.php
define('GCBLITE_COMPONENT_SERVER_URL', 'https://www.example.com.au');define('GCBLITE_RENDER_SECRET', getenv('RENDER_SECRET'));

You can also set the address with the gcblite_frontend_url filter, or in Settings → Gutenberg Control Blocks. A constant is locked to the file system, so it's the safest choice for production.

When a secret is set, WordPress sends it as an x-gcblite-render-secret header on every render call. Without it, anyone who finds your render route can use it, and they spend your server time.

3. Add the render route

Map each block name to a component:

wordpress/config/WPBlockRegistry.js
import SolaceBadge from '../../components/SolaceBadge'; export const WP_BLOCK_REGISTRY = {  'gcb/solace-badge': SolaceBadge,};

Then add the route WordPress calls. It reads the attributes from attrs, checks the secret, and wraps the output in <wp-block-wrapper>:

app/wordpress/render/[block]/page.jsx
import { headers } from 'next/headers';import { notFound } from 'next/navigation';import { WP_BLOCK_REGISTRY } from '@/wordpress/config/WPBlockRegistry'; const STARTED = Date.now(); export default async function Render({ params, searchParams }) {  const secret = (await headers()).get('x-gcblite-render-secret');  if (secret !== process.env.RENDER_SECRET) notFound();   const { block } = await params;  const attrs = JSON.parse((await searchParams).attrs || '{}');  const Block = WP_BLOCK_REGISTRY[`gcb/${block}`];  if (!Block) notFound();   return (    <wp-block-wrapper data-block-name={block} data-cache-timestamp={STARTED}>      <Block attributes={attrs} />    </wp-block-wrapper>  );}

The wrapper is the contract. WordPress keeps what's inside it and drops everything else in the response, such as the doctype, scripts and styles. data-cache-timestamp is optional, but set it: when your front end restarts, the new value clears WordPress's cached previews.

The component itself is ordinary React. It gets the block's fields as attributes:

components/SolaceBadge.jsx
export default function SolaceBadge({ attributes }) {  return <div className="sol-badge">{attributes.label}</div>;}

4. Design for a static preview

The editor preview is rendered once on the server and shown as HTML. React never hydrates in the editor. That shapes how you write a block:

  • Nothing that needs JavaScript to be seen. useEffect never runs, mount animations never play, and a closed accordion stays closed. Show the content by default, and use forceMount on collapsible Radix parts so their content is in the HTML.
  • Each block renders on its own. A child block is fetched without its parent. A component that reads context from a provider must bring its own provider, or the render fails.
  • Editor-only styles. Your front end publishes /wordpress/styles.css for the page and the editor canvas, and /wordpress/editor.css for the editor only. Put rules like "open every panel" in editor.css, under .editor-styles-wrapper.
  • Child blocks. Output a <Repeater> or <InnerBlocks> marker where children go. The editor swaps it for a real InnerBlocks area, with an Add button and drag to reorder.

5. Render the public pages

For visitors, your front end reads the page and renders the blocks itself:

  • Fetch the page. GCB adds a blocks_raw field to pages and posts in the REST API. It holds the saved block markup.
  • Parse it with @wordpress/block-serialization-default-parser, the parser WordPress uses. You get a tree of { blockName, attrs, innerBlocks }.
  • Fill in defaults. WordPress only saves attributes that differ from their defaults. Merge in the defaults from /wp-json/gcblite/v1/blocks.
  • Dispatch. Walk the tree, look up each block in WP_BLOCK_REGISTRY and render it. For blocks you haven't built a component for yet, post them to /wp-json/gcblite/v1/render-batch and WordPress renders them for you. That lets you move one block at a time.

The docs' examples use helpers such as getBlockDefaults and BlockRenderer. Those come from the gcb-next-starter repo, not from the plugin. Copy them, or write your own against the REST routes.

6. Refresh pages when your client saves

GCB can tell your front end which paths to revalidate when a post is saved. Set GCBLITE_REVALIDATE_URL, GCBLITE_REVALIDATE_SECRET and GCBLITE_REVALIDATE_ENABLED in wp-config.php, or use Settings → Gutenberg Control Blocks. To change the paths that are sent, use the gcblite_revalidate_paths filter.

WordPress and your front end can be on different hosts. They only need to reach each other over HTTPS: your front end calls the REST API, and WordPress calls your render route.

Try it

The docs cover each part in more depth: Rendering with a React component, the block parser, the block registry and deploying. The wp-config constants and REST API routes are listed in the reference. For an overview, see headless WordPress with visual editing.

Build anything in the block editor

Every licence includes all features and a year of updates and support.

Buy GCB from $8.25 a month See pricing