Building blocks
Writing a block
Each block has a tiny PHP/JSON schema in the theme and one React component in your Next.js (or Astro, or any HTTP-SSR) frontend. The same component renders both the editor preview and the public site — WordPress hits one route on your frontend to get the HTML when an author edits, and your frontend renders the same component for visitors.
Two ways to render a block:
- PHP
render.php— standard WP block. The plugin auto-wiresrender: file:./render.phpif it exists. - React component on your frontend — drop the
render.php. The plugin asks your frontend to render via its/wordpress/render/[block]route.
A block can use either; the plugin picks based on whether render.php
exists. See README.md for the overall architecture.
Where blocks live
themes/{active-theme}/blocks/{block-name}/
├── block.json # Standard WP block metadata
├── block.fields.json # GCB controls config (optional)
├── render.php # Server-side render (optional — see "Rendering with a React component")
└── style.css # Frontend + editor styles (optional)Block name = directory name. WP registers it as gcb/{block-name}.
What you write
block.json
Standard WP block metadata. Nothing GCB-specific in here:
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "gcb/team-grid",
"title": "Team Grid",
"category": "widgets",
"icon": "groups",
"textdomain": "gcb",
"attributes": {},
"supports": {}
}Rules:
supports:{}unless the block wants WordPress's own design tools. The ones that work throughget_block_wrapper_attributes()are safe on arender.phpblock whose root element prints it (the scaffold does):align,anchor,className,spacing,color,typography,dimensions,shadow. Both the canvas (its wrapper takes the rendered root's classes and style) and the page get them. Leave outlayout(it lays out the block's direct children, which on the canvas are GCB's repeater wrappers),html,lockand anything__experimental. A block rendered by a React component gets the support's attributes as props but has to apply them itself — keep{}there unless it does.attributesshould be{}— they're auto-generated fromblock.fields.json.renderis auto-wired by the plugin ifrender.phpexists; no need to set it.
block.fields.json
GCB-specific. Declares Inspector controls; the plugin auto-generates the WP attribute defs from these.
{
"controls": [
{
"id": "panel_main",
"type": "group",
"label": "Main",
"controlsGroup": "settings"
},
{
"id": "ctrl_heading",
"type": "text",
"label": "Heading",
"attributeKey": "heading",
"controlsGroup": "settings",
"parentPanelId": "panel_main"
}
]
}For controls: id is unique within the block; attributeKey is required for non-group types.
The schema file is at schemas/gcb.schema.json — point your editor at it via $schema in your block.fields.json for autocomplete (optional).
Words first. The editor shows the panel that holds a block's words (its text, textarea, richtext, wysiwyg,
url/link controls) first and opens it, because words are what people edit most. Other panels
keep the order you wrote. Put the words in their own panel. Settings that only matter in one mode get
conditionalLogic, so a choice that makes another meaningless hides it.
IMPORTANT: picking the right control type
Get this wrong and the attribute ends up the wrong shape (e.g. array instead
of string), the component reads undefined, and the block looks broken.
Pick by the shape of the saved value, not by what looks nicest in the UI:
| You want… | Control | Stored type | Notes |
|---|---|---|---|
| Free-text single line | text | string | |
| Free-text multi-line | textarea | string | |
| Rich text (bold, links, lists) | wysiwyg | string | HTML. Often better as InnerBlocks — see below. |
| On/off toggle | toggle | boolean | A real switch. Not a checkbox. |
| One-of-N from a small fixed set (≤4, visual) | toggle-group | string | Segmented control. Single-value. Use for left/right. |
| One-of-N from a larger set | select | string | Dropdown. |
| One-of-N visible all at once | radio | string | Stacked radio buttons. |
| Many-of-N (checkbox column) | checkbox-group | array | Vertical list of checkboxes. |
| Many-of-N (button row) | button-group | array | Horizontal row of toggle buttons. SAME data shape as checkbox-group (multi-select / array). NOT single-value — use toggle-group for that. |
| Single boolean question | checkbox | boolean | When a toggle switch is the wrong affordance. |
| Number | number | number | Free-typed. |
| Number on a fixed scale | range | number | Slider with min/max/step. |
| Colour | color | string | Hex / theme palette. |
| Single image | image | object | { id, url, alt, width, height, ... }. |
| Gallery of images | gallery | array | [{ id, url, alt, ... }, ...]. |
| Other media (PDF, video, etc.) | file | object | |
| Linked post / page | post-object | object | |
| Linked URL | url | object | |
| Date | date | string | ISO date. Opens a calendar in a popover/modal. |
| Date + time | datetime | string | ISO datetime. Opens calendar + hour/minute picker (DateTimePicker). |
| Heading (text + level) | heading-level | object | Compound: text input + level select. Stored as { text, level }. Default offers H1-H6 + P + DIV + SPAN; restrict via levels: ['h2', 'h3']. React: const T = h.level; return <T>{h.text}</T>;. |
| Icon picker | icon | object | |
| Code snippet | code | string |
Pins on a picture: hotspots or pin-map. Both put numbered pins on an
image; they differ in where a pin's content lives.
| Use | When | The pins are |
|---|---|---|
hotspots | Each pin carries a few short fields (a title, a line of text) and nothing else. | Rows inside the one field — { image, pins: [{ point, … }] }. Edited in the sidebar. |
pin-map | Each pin opens a real card: its own block, with fields, an icon, rich content, its own canvas editing. | The block's repeater children. The field holds only the picture; each child holds its point (or, with pointsKey, a list of locations — 06a, 06b). Adding a pin adds a card. |
point | One spot on an image the block already has (a focal point, a single marker). | One { x, y }. |
Rule of thumb: if you would want to edit a pin's content on the canvas, or
give it more than three fields, it is a pin-map. Don't use both on one
block.
Bias toward InnerBlocks for prose-y content. If the field is going to
hold paragraphs of text — let alone paragraphs + lists + images — use a
free InnerBlocks slot in the React component instead of a wysiwyg
control. Authors get all of Gutenberg's tools, not a cramped Inspector
textarea. See "Rendering with a React component" below for how blocks emit
the <innerblocks> marker.
Common AI mistakes to avoid:
- Don't reach for
button-groupfor left/right or on/off. It's a multi-select (same array shape ascheckbox-group, just rendered as a row of toggle buttons). Usetoggle-group(single-value segmented) ortoggle(boolean switch). - Don't use
checkboxfor "is this enabled" —togglereads as a real switch and is the WP-native affordance for on/off. - If a control should store a non-default
attributeType(e.g. atextfield that should benumber), set it explicitly. The auto-detection has sensible defaults but they're per-control-type, not per-use-case. - When in doubt, prefer fewer controls + an InnerBlocks slot in the component. WordPress is good at block authoring; the Inspector is best for short typed atoms.
IMPORTANT: discover the full control surface before using it
The table above tells you which control to pick. It does not list
every field a control stores. Rich controls like image, gallery,
url, icon, spacing, size, post-object, relationship, etc.
store multi-field objects with toggleable features (focal point,
size mode, custom width, isFixed, etc.).
If you build a component that reads only image.url and image.alt,
you have silently dropped focal-point, cover/contain, custom width,
fixed-background — every other feature the author can configure. The
block ships looking broken.
Rule when using any control in block.fields.json or reading its
value in a React component:
Before you write the code, Read the file
src/controls/{type}.jsin the plugin. The header JSDoc comment is the authoritative API contract — stored shape, every field, every feature toggle.
This is on purpose: hand-maintained docs go stale the moment a control changes. The control file is the source of truth. Reading it costs one tool call; getting it wrong costs a re-implementation.
When wiring a component:
- Read
src/controls/{type}.jsfor the stored shape. - Honour every documented field, not just the obvious ones. If the
control stores a focal point, your
<img>should setobject-position. If it storessize, setobject-fit. If it storescustomWidth, use inline width. IfisFixed, setbackground-attachment: fixedwhen used as a background. - If a feature genuinely doesn't apply to this block's use case (e.g.
the image is foreground-only so
isFixedis meaningless), add a one- line comment explaining the omission. That way the next person doesn't wonder if it was forgotten.
Defaults reasonable when the field is absent (focal point defaults to
0.5/0.5; size defaults to cover; customWidth empty means no override).
render.php
Standard WP render callback. Receives $attributes, $content, $block in scope:
<?php
$heading = $attributes['heading'] ?? '';
$wrapper_attributes = get_block_wrapper_attributes(['class' => 'gcb-team-grid']);
?>
<div <?php echo $wrapper_attributes; ?>>
<?php if ($heading) : ?>
<h2><?php echo esc_html($heading); ?></h2>
<?php endif; ?>
<?php echo $content; ?>
</div>Always get_block_wrapper_attributes() and echo it on the root element. Always escape dynamic values.
What you do not write
- No
edit.js/save.js— the plugin renders the Inspector UI fromgcb.controls. - No hand-written
attributesinblock.json— they're derived. - No webpack config per block.