Introduction to Block Development
Welcome to Block Development
The WordPress Block Editor (Gutenberg) has fundamentally changed how content is created in WordPress. As a developer, understanding block development opens up powerful possibilities for custom content experiences.
In this tutorial, you'll build real-world custom blocks including:
- A testimonial block with avatar and rating
- A pricing table block with dynamic features
- A posts grid block with filtering
- Interactive blocks with frontend JavaScript
What is a Gutenberg Block?
A block is an atomic unit of content in the editor. Every paragraph, image, heading, and embed is a block. Blocks are:
- Self-contained โ Each block manages its own rendering
- Configurable โ Users can customize via toolbar and sidebar
- Reusable โ Blocks can be saved as patterns
- Transformable โ Convert between block types
Block Architecture
Every block consists of these parts:
| Component | Purpose |
|---|---|
block.json | Metadata, attributes, supports |
edit.js | React component for editor |
save.js | Output saved to database |
index.js | Registration and exports |
style.css | Frontend styles |
editor.css | Editor-only styles |
Setting Up Your Environment
You need Node.js 18+ and npm. We'll use the official @wordpress/create-block scaffolding tool:
cd wp-content/plugins
npx @wordpress/create-block testimonial-block
cd testimonial-block
npm startThis creates:
testimonial-block/
โโโ build/ # Compiled assets
โโโ src/
โ โโโ block.json # Block metadata
โ โโโ edit.js # Editor component
โ โโโ save.js # Save component
โ โโโ index.js # Registration
โ โโโ style.scss # Frontend styles
โ โโโ editor.scss # Editor styles
โโโ testimonial-block.php # Plugin file
โโโ package.jsonThe block.json File
This is the heart of your block definition:
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "theme/testimonial",
"version": "1.0.0",
"title": "Testimonial",
"category": "widgets",
"icon": "format-quote",
"description": "Display customer testimonials with rating.",
"keywords": ["quote", "review", "feedback"],
"supports": {
"html": false,
"align": ["wide", "full"],
"color": {
"background": true,
"text": true
}
},
"attributes": {
"content": {
"type": "string",
"source": "html",
"selector": ".testimonial-content"
},
"author": {
"type": "string",
"default": ""
},
"rating": {
"type": "number",
"default": 5
}
},
"textdomain": "testimonial-block",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css"
}Understanding Attributes
Attributes are the block's data model. They define what's stored and where it comes from:
"attributes": {
"title": {
"type": "string",
"default": "Hello World"
},
"items": {
"type": "array",
"default": []
},
"mediaId": {
"type": "number"
},
"mediaUrl": {
"type": "string",
"source": "attribute",
"selector": "img",
"attribute": "src"
}
}| Source | Description |
|---|---|
html | Inner HTML of selector |
text | Text content of selector |
attribute | DOM attribute value |
query | Array from multiple elements |
| (none) | Stored in block comment |
Block Supports
Enable built-in features without custom code:
"supports": {
"align": true,
"anchor": true,
"className": true,
"color": {
"background": true,
"text": true,
"link": true,
"gradients": true
},
"spacing": {
"margin": true,
"padding": true
},
"typography": {
"fontSize": true,
"lineHeight": true
}
}Development Workflow
Start the build watcher:
npm startThis watches for changes and rebuilds automatically. For production:
npm run buildNext Steps
In the next lesson, we'll build the Edit component using React, handling user input and block attributes.