Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block with React?
Asked on Dec 08, 2025
Answer
Creating a custom Gutenberg block with React involves using the WordPress Block Editor's JavaScript APIs. You'll need to set up a development environment with Node.js and npm to build and register your block.
<!-- BEGIN COPY / PASTE -->
// Step 1: Install @wordpress/scripts
npm install @wordpress/scripts --save-dev
// Step 2: Create a block.js file
import { registerBlockType } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';
registerBlockType('my-plugin/custom-block', {
title: 'Custom Block',
icon: 'smiley',
category: 'common',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'p',
},
},
edit: ({ attributes, setAttributes }) => {
return (
<RichText
tagName="p"
value={attributes.content}
onChange={(content) => setAttributes({ content })}
/>
);
},
save: ({ attributes }) => {
return <RichText.Content tagName="p" value={attributes.content} />;
},
});
// Step 3: Add build script in package.json
"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start"
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure Node.js and npm are installed on your system.
- Use the command "npm start" to run a development server that watches for changes.
- Register your block in a WordPress plugin or theme by enqueuing the compiled JavaScript file.
- Refer to the WordPress Block Editor Handbook for more details on block development.
Recommended Links:
