Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block in WordPress?
Asked on Nov 07, 2025
Answer
Creating a custom Gutenberg block in WordPress involves using the block editor's JavaScript and PHP APIs. This allows you to add custom functionality and design elements to your WordPress site.
<!-- BEGIN COPY / PASTE -->
// JavaScript: Register a custom block
wp.blocks.registerBlockType('my-plugin/my-custom-block', {
title: 'My Custom Block',
icon: 'smiley',
category: 'common',
edit: function(props) {
return wp.element.createElement(
'p',
{ className: props.className },
'Hello, Gutenberg!'
);
},
save: function() {
return wp.element.createElement(
'p',
null,
'Hello, Gutenberg!'
);
}
});
// PHP: Enqueue block scripts
function my_custom_block_assets() {
wp_enqueue_script(
'my-custom-block',
plugins_url('block.js', __FILE__),
array('wp-blocks', 'wp-element')
);
}
add_action('enqueue_block_editor_assets', 'my_custom_block_assets');
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have Node.js and npm installed to set up a build process for your block.
- Use the WordPress CLI or a starter plugin to scaffold your block setup.
- Consider using the @wordpress/scripts package to simplify the build configuration.
- Test your block thoroughly in the Gutenberg editor to ensure compatibility.
Recommended Links:
