What is a Block?
A “block” is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage. The idea combines concepts of what in WordPress we traditionally achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience.Block Registration
Every block starts by registering a new block type definition using theregisterBlockType function from the @wordpress/blocks package.
Basic Block Structure
Block Name
The name for a block is a unique string that identifies it. Names must be structured asnamespace/block-name, where namespace is the name of your plugin or theme.
Important: Block names cannot be changed later without consequences. The block name is stored in the post content, so changing it requires editing all affected posts or running database scripts.
Namespace Best Practices
- Use your actual plugin/theme name:
my-awesome-plugin/block-name - Avoid generic names like
editorial/,block/, orcreate-block/ - Use the same namespace for all blocks in your plugin/theme
- Make it unique to prevent conflicts with other plugins
Block Configuration Properties
Required Properties
title
The display title for your block, which can be translated. Appears in the inserter and throughout the editor.category
Blocks are grouped into categories. Core categories include:textmediadesignwidgetsthemeembed
Optional Properties
description
A short description shown in the Block Tab in the Settings Sidebar.icon
An icon to identify your block. Can be a Dashicon slug or custom SVG.keywords
Terms that help users discover your block while searching.The Edit Function
Theedit function defines what users see in the block editor when editing content.
Using useBlockProps
Always useuseBlockProps() to ensure your block works correctly with the editor:
useBlockProps hook:
- Adds necessary CSS classes
- Handles block supports attributes
- Ensures proper editor integration
Accessing Attributes
The Save Function
Thesave function defines the final markup saved to the database and rendered on the frontend.
Always use
useBlockProps.save() in the save function to ensure attributes from block supports are properly saved.Installation
To use the blocks package:Creating Your First Block
1
Set up your development environment
Install Node.js and npm, then set up a build process for JSX.
2
Create your block registration file
Create a new JavaScript file and import the necessary functions:
3
Register your block
Call
registerBlockType with your block name and configuration:4
Enqueue your script
Register and enqueue your block script in PHP:
5
Test your block
Open the WordPress editor and insert your new block from the inserter.
Block Collections
You can group related blocks into collections:my-plugin namespace will appear under this collection in the inserter.
Common Block Patterns
Simple Content Block
Next Steps
- Learn about Block Attributes to add data to your blocks
- Explore Block Supports to add editor features
- Understand Dynamic Blocks for server-side rendering
- Master Nested Blocks to create container blocks