WordPress
Building a Custom Gutenberg Block from Scratch

Gutenberg blocks are the backbone of modern WordPress content editing. But the documentation can be sparse, and the JavaScript build tooling has changed enough times that most tutorials are out of date within a year. This guide covers what actually works in 2026.
Step 1: Register the block type
Start with register_block_type from the @wordpress/blocks package. You'll need three things: the block name (namespaced, like my-plugin/my-block), the edit component, and the save function.
Step 2: Build the edit component
The edit component is what renders in the editor. It receives attributes and setAttributes — think of attributes as your block's state. Define a schema that tells WordPress what to save to post content.
- Use
useBlockPropsto wrap your output — it handles the class names and data attributes WordPress expects. - Keep the edit function pure. Side effects belong in
useEffect, not in the render path. - Test with rich text content. The
RichTextcomponent handles serialization, but edge cases with nested formatting trip up most first drafts.
Step 3: The save function
Save returns static HTML — no hooks, no state, no components. Whatever you return gets stored in post_content. If your save function references a component that doesn't exist at render time, the block breaks silently.
Step 4: Enqueue and test
Register your block in init hooked function. Use block.json for metadata — it's cleaner than PHP arrays and IDE autocomplete works properly. Then test in the editor with various content types: empty state, rich text, nested blocks, and server-side rendered content.
The block editor is a hostile environment. Caching plugins, script concatenation, and aggressive minification will break your blocks. Always test with a vanilla WordPress install before declaring victory.