Basics of the registerBlockType function
In this article we will focus on the basics of what a block is and how they are registered. We will not cover how to set up your environment and project for block development. We will do this in a later post!
You register a block in javascript with the function registerBlockType (found in ‘@wordpress/blocks’). The function takes a name and a block object as parameters. The block object basically contains:
- Meta data. This includes name, category and attributes that the block will use.
- Edit function. A React functional component that is run in the editor when you create your pages and posts.
- Save function. A React functional component that is run when you click on save. WordPress will save the html that is returned from this function into the database with your post.
Both the save & edit functions return jsx. Below we register a ‘Hello World’ block:
import { registerBlockType } from '@wordpress/blocks';
registerBlockType("your-namespace/helloworld", {
category: "widgets",
attributes: {},
title: 'Hello World',
icon: 'block-default',
keywords: ['sample-block', 'hello world'],
edit: (props) => {
const { attributes, setAttributes } = props; // you will use these!
return (
<div>
Hello World (in the editor)!
</div>
);
},
save: (props) => {
const { attributes } = props; // you will use these!
return (
<div>
Hello World!
</div>
);
},
});
For this block to actually be useful we will need to add editing capabilities. This is done by either adding inline-editing with the ‘toolbar’ or adding editing controls on the right sidebar. Here is a short intro to coding the inspector controls (adding controls to the editor sidebar).
When you create larger blocks, you will put the edit & save function in seperate files and import them when you register your block registration. That is why you will find edit.js & save.js snippets in other tutorials on Wpblockz.
For a complete plugin, register the block metadata on the server and build/enqueue the editor script as well. Start with the official Block Editor getting-started guide; the example here focuses on the registration object.
Static and dynamic output
A static block saves its markup through save. A dynamic block usually returns null from save and renders with PHP. Keep the saved markup stable to avoid validation errors when editing old content. Our dynamic-block tutorial covers that second approach.



