In this article11 sections
The block editor (Gutenberg) is built from blocks: paragraphs, images, buttons. Creating your own custom Gutenberg block lets you offer clients and editors a ready-made component, such as a testimonial box, a pricing card or a call-to-action.
You do not have to set up the tooling by hand. WordPress provides an official tool called create-block that generates a working plugin for you. This guide walks through your first block.
What you need
- A local WordPress site (LocalWP or similar).
- Node.js and npm installed. Use a current long-term support (LTS) version of Node.
- Basic JavaScript. Some React knowledge helps, but you can learn as you go.

Step 1: Generate the block plugin
Open a terminal, go into your site’s wp-content/plugins folder and run:
npx @wordpress/create-block@latest my-first-block
cd my-first-block
The tool creates a folder with a full plugin: a PHP file, a block.json file, a src folder with JavaScript and styles, and a build setup.
Step 2: Start the development build
npm start
This watches your files and rebuilds whenever you save. Leave it running. In the WordPress dashboard, activate the new plugin, then open the editor and add the block by searching for its name.
Step 3: Understand the files
block.jsondescribes the block: its name, title, category, attributes and which files to load. This is the single most important file.src/index.jsregisters the block.src/edit.jsis what editors see and use inside the editor.src/save.jsis the HTML saved into the post and shown to visitors.src/style.scssis the front-end and editor style, andeditor.scssis editor-only.
Step 4: Add an editable text field
Let’s turn the starter block into a simple message box. First, define an attribute in block.json:
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": "p"
}
}
Then change edit.js:
import { __ } from '@wordpress/i18n';
import { useBlockProps, RichText } from '@wordpress/block-editor';
export default function Edit( { attributes, setAttributes } ) {
return (
<RichText
{ ...useBlockProps() }
tagName="p"
value={ attributes.message }
onChange={ ( message ) => setAttributes( { message } ) }
placeholder={ __( 'Write a message…', 'my-first-block' ) }
/>
);
}
And save.js:
import { useBlockProps, RichText } from '@wordpress/block-editor';
export default function save( { attributes } ) {
return (
<RichText.Content
{ ...useBlockProps.save() }
tagName="p"
value={ attributes.message }
/>
);
}
Save the files, reload the editor and add the block again. You can now type into it, and the text is saved with the post.
Step 5: Build for production
When you are done developing, stop the watcher and run:
npm run build
This creates optimised files in the build folder, which is what WordPress loads on a live site. Zip the plugin folder (without node_modules) to install it elsewhere.
Static vs dynamic blocks
The block above is static: its HTML is saved in the post. That is fine for simple content. A dynamic block renders on the server each time, using PHP, which suits content that changes, such as “latest posts” or “current price”. The create-block tool can generate one with a --variant dynamic option, and block.json then points to a render.php file.
Common problems and fixes
- “This block contains unexpected or invalid content.” You changed the HTML in
save.jsafter the block was already used in a post. WordPress compares saved HTML with whatsaveoutputs now. For existing content, add a deprecation or recreate the block. - Block not showing in the editor. Check that the plugin is active and that
npm startornpm run buildhas produced files inbuild. - Styles not loading. Check the
styleandeditorStyleentries inblock.json. - Node errors on install. Use a supported Node.js version and delete
node_modulesbefore reinstalling.
Tips before you ship
- Change the default block namespace from
create-blockto your own, such asmy-company/message-box. - Add a good name, icon and description in
block.json, since editors search by them. - Test the block with several themes.
- Write escape-safe PHP in any
render.phpfile. Our security checklist is a good habit list.
Frequently asked questions
Do I need to know React?
Not at first. The generated files give you a working starting point. React knowledge helps once you add more complex interfaces.
Can I make a block without Node.js?
Yes, with plain JavaScript and wp.blocks.registerBlockType, but the create-block tool is much easier and is the recommended way.
Will my block work with every theme?
The block itself will load, but its look depends on your styles and the theme’s. Test it on at least two themes.
Summary
Run create-block, start the dev build, define attributes in block.json, write your edit and save functions, then build for production. To set up the plugin around it, read how to create a WordPress plugin from scratch.












