GCBGutenberg
Control Blocks
v1.3.3
Buy

How to build a block with Gutenberg Control Blocks

Two ways to build a block: three plain files in your theme's blocks/ folder, or the template as a component in your own front end. The editor is 1:1 with the page either way.

There are two ways to build a block with Gutenberg Control Blocks (GCB). Both start from the same two files that describe the block. They differ in where its template lives.

  1. In your theme. Add a folder to your theme's blocks/ folder with three plain files: block.json, block.fields.json and render.php. The editor draws the block from that same template, so what your client edits is 1:1 with the page.
  2. In your own front end. Keep block.json and block.fields.json in the theme, and write the template as a component in the front end you're building: React, Vue, Svelte or anything that returns HTML. The editor asks your front end to draw the block, so it's still 1:1.

You don't register the block in either. There's no register_block_type() call and nothing to add to functions.php: GCB finds the folder and registers the block for you.

Either way a block is code you write and keep in Git. There's no drag-and-drop designer and no template editor in wp-admin: the template is a file.

Way one: three files in your theme

Create a folder in your theme's blocks/ folder. The folder name is the block's name, so blocks/solace-badge/ registers as gcb/solace-badge.

1. block.json is WordPress's standard block metadata, the same file a core block has. Leave attributes out: GCB makes them from your fields.

blocks/solace-badge/block.json
{  "apiVersion": 3,  "name": "gcb/solace-badge",  "title": "Solace badge",  "category": "widgets",  "icon": "tag"}

2. block.fields.json lists the block's fields. Each has a type (one of 42), a label and the attribute it saves to.

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

3. render.php is the template. It reads the values from $attributes, the same as a core block's PHP render. Put data-gcb-field on anything your client should edit where it sits on the page.

blocks/solace-badge/render.php
<?php $label = esc_html($attributes['label'] ?? ''); ?><div <?php echo get_block_wrapper_attributes(['class' => 'sol-badge']); ?>>  <span data-gcb-field="label" data-gcb-field-type="text"><?php echo $label; ?></span></div>

Save the files and reload the editor. The block is in the inserter. That's the last step: you don't register it, GCB found the folder. There's no edit.js, no npm and no build step: the editor shows the output of the same render.php your visitors get, and builds the sidebar from block.fields.json.

If you'd rather not create the files by hand, scaffold them. Both of these write the same folder:

  • the command line: wp gcb scaffold team-grid --controls="heading:text,intro:textarea"
  • wp-admin: Gutenberg Control Blocks, then New, where you name the block and pick its fields from a list.

Do I need to register the block?

No. Don't call register_block_type() for a GCB block, and don't add anything to functions.php. GCB looks in the active theme's blocks/ folder each time WordPress loads and registers every folder that has a block.json and a block.fields.json. Add a folder and the block exists. Delete the folder and it's gone. There's no list of blocks to keep anywhere.

The same goes for the template: leave the render key out of block.json. If the folder has a render.php, GCB uses it.

Way two: the template in your own front end

If your site's front end is React, Vue, Svelte or another framework, the block's template can live there. The block still has its block.json and block.fields.json in the theme. Leave render.php out.

1. Tell WordPress where your front end is, in wp-config.php:

wp-config.php
define( 'GCB_COMPONENT_SERVER_URL', 'https://solace.com.au' );

2. Write the block as a component in your front end, with the same data-gcb-field markers:

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

3. Give it an address WordPress can ask. When the editor needs to draw the block, GCB requests /wordpress/render/solace-badge from your front end with the block's saved values, and puts the HTML that comes back on the canvas. The Next.js guide has that route ready to copy. Any framework works, as long as it can answer that request with HTML.

Your client clicks the label and types, in the block editor, on your real component. Your pages render the same component from the blocks WordPress saves.

Which to use

  • A WordPress theme draws the site: way one. This is most sites, and it needs nothing but the plugin.
  • A separate front end draws the site (headless): way two.
  • Both on one site is fine. A block with a render.php is drawn by WordPress; a block without one is drawn by your front end.

Next

The callout tutorial builds one small block start to finish, with every file. The Quickstart builds a first block end to end. The field reference lists all 42 field types. The examples are finished blocks you can download and read.

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