Black Friday Save 45% on any WPAssist Hub plugin See deals
WordPress Development
By WPAssist Hub TeamUpdated Nov 26, 20264 min read

How to Create Your First Gutenberg Block in WordPress

Illustration of a building block snapping into the WordPress editor
In this article11 sections
  1. What you need
  2. Step 1: Generate the block plugin
  3. Step 2: Start the development build
  4. Step 3: Understand the files
  5. Step 4: Add an editable text field
  6. Step 5: Build for production
  7. Static vs dynamic blocks
  8. Common problems and fixes
  9. Tips before you ship
  10. Frequently asked questions
  11. Summary

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.
Infographic showing five steps to build a custom Gutenberg block

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.json describes the block: its name, title, category, attributes and which files to load. This is the single most important file.
  • src/index.js registers the block.
  • src/edit.js is what editors see and use inside the editor.
  • src/save.js is the HTML saved into the post and shown to visitors.
  • src/style.scss is the front-end and editor style, and editor.scss is 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.js after the block was already used in a post. WordPress compares saved HTML with what save outputs 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 start or npm run build has produced files in build.
  • Styles not loading. Check the style and editorStyle entries in block.json.
  • Node errors on install. Use a supported Node.js version and delete node_modules before reinstalling.

Tips before you ship

  • Change the default block namespace from create-block to your own, such as my-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.php file. 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.

WPAssist Hub
Written byWPAssist Hub Team

We build lightweight WordPress plugins and write practical, tested guides for site owners and developers. Our team is led by a WordPress Core Contributor with 6+ years of WordPress experience.

About us →

Leave a Comment

Your email address will not be published. Required fields are marked *