Creating a custom Gutenberg block in WordPress is easier than you think. This guide will show you how. Let’s get started.
What is a Gutenberg Block?
Gutenberg is the new editor in WordPress. It uses blocks to create content. Each piece of content is a block. For example, a paragraph, an image, or a video.
Why Create a Custom Block?
Sometimes, the default blocks are not enough. You may want something special. A custom block can do that. It can add unique features to your site.
Tools You Need
- WordPress installed
- Basic knowledge of HTML, CSS, and JavaScript
- A text editor like VS Code
- Node.js installed

Credit: torquemag.io
Step 1: Set Up Your Environment
First, you need to set up your environment. Follow these steps:
- Open your terminal or command prompt.
- Navigate to your WordPress theme folder.
- Create a new folder for your block.
- Run
npm init
to create apackage.json
file. - Install the required packages with
npm install @wordpress/scripts --save-dev
.

Credit: positiwise.com
Step 2: Create the Block Files
Now, you need to create the block files. Follow these steps:
- Create a new file called
block.json
. This file will describe your block. - Create a new folder called
src
. Inside this folder, create a file calledindex.js
. This file will contain your block code. - Create a new file called
style.css
. This file will contain your block styles. - Create a new file called
editor.css
. This file will contain your block editor styles.
Step 3: Define the Block
Next, you need to define your block. Follow these steps:
- Open
block.json
. Add the following code:
{
"apiVersion": 2,
"name": "custom/block-name",
"title": "Custom Block",
"category": "common",
"icon": "smiley",
"description": "A custom block.",
"attributes": {
"content": {
"type": "string",
"source": "html",
"selector": "p"
}
},
"supports": {
"html": false
},
"textdomain": "custom-block",
"editorScript": "file:./build/index.js",
"editorStyle": "file:./editor.css",
"style": "file:./style.css"
}
Step 4: Register the Block
Now, you need to register your block. Follow these steps:
- Open
index.js
. Add the following code:
import { registerBlockType } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';
registerBlockType( 'custom/block-name', {
edit: ( props ) => {
const { attributes: { content }, setAttributes } = props;
const onChangeContent = ( newContent ) => {
setAttributes( { content: newContent } );
};
return (
);
},
save: ( props ) => {
const { attributes: { content } } = props;
return ;
},
} );
Step 5: Add Styles
Next, you need to add styles. Follow these steps:
- Open
style.css
. Add your block styles. - Open
editor.css
. Add your editor styles.
Step 6: Build the Block
Now, you need to build your block. Follow these steps:
- Open your
package.json
file. - Add the following script:
"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start"
}
- Run
npm run build
to build your block.
Step 7: Test Your Block
Finally, you need to test your block. Follow these steps:
- Open your WordPress admin panel.
- Create or edit a post.
- Add your custom block. Check if it works as expected.
Frequently Asked Questions
How Do I Start Creating A Custom Gutenberg Block?
Start by installing the necessary tools like Node. js and npm. Then set up your development environment.
What Tools Do I Need To Create A Custom Block?
You need Node. js, npm, and WordPress’s block editor. These are essential for development.
Can I Create A Custom Block Without Coding Skills?
Basic coding knowledge is required. You need to understand JavaScript, PHP, and WordPress functions.
How Long Does It Take To Create A Custom Block?
It depends on your coding skills. Usually, it takes a few hours to a day.
Conclusion
Creating a custom Gutenberg block in WordPress is not hard. Follow these steps. You will have your custom block ready soon. Happy coding!