Custom Gutenberg Block Development: React JSX & block.json

The introduction of the Gutenberg Block Editor fundamentally transformed WordPress into a modern React.js single-page application (SPA). While legacy shortcodes relied on clunky PHP text parsing, modern enterprise WordPress engineering demands creating bespoke Custom Gutenberg Blocks. In this class, you will learn the modern WordPress 6.x standard for building blocks using React JSX, modern build tools (@wordpress/scripts), and the block.json metadata specification.

Gutenberg Block Architecture Diagram showing block.json, edit JSX, and save HTML

Figure 38.1: Full Gutenberg Block Engineering Pipeline

Step 1: Scaffolding a Block with @wordpress/create-block

The official, recommended way to scaffold a modern Gutenberg block plugin is using the WordPress core team's official Node.js generator. In your terminal, navigate to /wp-content/plugins/ and execute:

# Scaffolding a custom block plugin via npm
npx @wordpress/create-block veewom-cta-box --variant=static

This single command configures a complete TypeScript/Babel/Webpack build pipeline (via @wordpress/scripts), installs NPM dependencies, and creates the following production architecture:

/veewom-cta-box/
├── block.json       # Block Metadata (API v3)
├── package.json     # Node build scripts
├── src/
│   ├── block.json
│   ├── edit.js      # React Component rendered inside wp-admin
│   ├── save.js      # HTML rendered on public frontend
│   ├── index.js     # Block Registration
│   ├── style.scss   # Frontend + Backend styles
│   └── editor.scss  # Backend Editor-only styles
└── build/           # Compiled production bundle

Step 2: Defining Metadata in block.json (API Version 3)

In WordPress 6.x, block.json is the absolute single source of truth for your block's attributes and capabilities:

{
  "$schema": "https://schemas.wp.org/trunk/block.json",
  "apiVersion": 3,
  "name": "veewom/cta-box",
  "version": "1.0.0",
  "title": "Veewom Call to Action Box",
  "category": "design",
  "icon": "megaphone",
  "description": "A high-converting CTA box with customizable title and button.",
  "supports": {
    "html": false,
    "color": {
      "background": true,
      "text": true
    },
    "spacing": {
      "margin": true,
      "padding": true
    }
  },
  "attributes": {
    "heading": {
      "type": "string",
      "default": "Ready to Learn WordPress?"
    },
    "buttonText": {
      "type": "string",
      "default": "Get Started Today"
    },
    "buttonUrl": {
      "type": "string",
      "default": "https://veewom.com"
    }
  },
  "editorScript": "file:./build/index.js",
  "editorStyle": "file:./build/index.css",
  "style": "file:./build/style-index.css"
}

Step 3: The Edit Component (edit.js — Admin Interface)

The edit() function is a React functional component rendered inside the Gutenberg canvas. It utilizes the useBlockProps hook and WordPress UI components (RichText, InspectorControls):

import { __ } from '@wordpress/i18n';
import { useBlockProps, RichText, InspectorControls } from '@wordpress/block-editor';
import { PanelBody, TextControl } from '@wordpress/components';

export default function Edit( { attributes, setAttributes } ) {
    const { heading, buttonText, buttonUrl } = attributes;
    const blockProps = useBlockProps( { className: 'veewom-cta-container' } );

    return (
        <>
            <InspectorControls>
                <PanelBody title={ __( 'CTA Link Settings', 'veewom' ) }>
                    <TextControl
                        label={ __( 'Button Target URL', 'veewom' ) }
                        value={ buttonUrl }
                        onChange={ ( val ) => setAttributes( { buttonUrl: val } ) }
                    />
                </PanelBody>
            </InspectorControls>

            <div { ...blockProps }>
                <RichText
                    tagName="h3"
                    value={ heading }
                    onChange={ ( val ) => setAttributes( { heading: val } ) }
                    placeholder={ __( 'Enter CTA Headline...', 'veewom' ) }
                />
                <RichText
                    tagName="span"
                    className="veewom-btn-preview"
                    value={ buttonText }
                    onChange={ ( val ) => setAttributes( { buttonText: val } ) }
                />
            </div>
        </>
    );
}

Step 4: The Save Function (save.js — Frontend HTML)

The save() function defines the static HTML written to the MySQL database (wp_posts.post_content):

import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
    const { heading, buttonText, buttonUrl } = attributes;
    const blockProps = useBlockProps.save( { className: 'veewom-cta-container' } );

    return (
        <div { ...blockProps }>
            <RichText.Content tagName="h3" value={ heading } />
            <a href={ buttonUrl } className="veewom-cta-btn">
                { buttonText }
            </a>
        </div>
    );
}

Dynamic vs. Static Gutenberg Blocks

In senior developer interviews: "What is the difference between a static block and a dynamic block?" Static blocks save compiled HTML into the database via save.js (fastest, zero server runtime). Dynamic blocks return null in save.js and define a PHP callback (via render.php or render_callback in register_block_type) that runs on the server for each request, essential for querying live content like "Latest 5 Comments".

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Do I need to compile JavaScript when developing Gutenberg blocks?

Yes. Modern Gutenberg development uses React JSX and ESNext syntax, which browsers cannot execute natively. You run `npm run start` during local development (for real-time live reloading) and `npm run build` to output minified production assets in the `/build/` folder.

Q2. What is Block Invalidation and how do I prevent it?

Block invalidation occurs when the HTML saved in the database no longer matches the HTML generated by your current `save()` function. To update a block without invalidating existing posts, use Gutenberg's `deprecated` array property to preserve backwards compatibility.

Q3. Can I use PHP without Node.js to create Gutenberg blocks?

Yes! Using the popular plugin **Advanced Custom Fields (ACF Pro)**, developers can register custom Gutenberg blocks using pure PHP (`acf_register_block_type()`) and standard PHP template files without setting up a Node.js compiler.

Explore Related WordPress Tutorials & Next Steps

Expand your CMS mastery by studying these tightly integrated WordPress development topics:

Gutenberg Block Editor Guide

Understand block controls, inspector panels, and user block interactions.

Custom Plugin Development

Package your custom blocks into standalone, reusable plugins.

Full Site Editing (FSE)

Use custom blocks inside Block Themes and FSE templates.

Headless Decoupled WordPress

Parse block markup into JSON objects for Next.js frontend rendering.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!