Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block in WordPress?
Asked on Nov 23, 2025
Answer
Creating a custom Gutenberg block in WordPress involves using JavaScript and the WordPress Block API to define the block's behavior and appearance. You'll need to enqueue the necessary scripts and styles in your theme or plugin.
<!-- BEGIN COPY / PASTE -->
// Enqueue block scripts
function my_custom_block() {
wp_register_script(
'my-block-script',
get_template_directory_uri() . '/js/my-block.js',
array('wp-blocks', 'wp-element', 'wp-editor'),
filemtime(get_template_directory() . '/js/my-block.js')
);
register_block_type('myplugin/my-custom-block', array(
'editor_script' => 'my-block-script',
));
}
add_action('init', 'my_custom_block');
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have Node.js and npm installed to manage JavaScript dependencies.
- Create a JavaScript file (e.g., my-block.js) where you define the block using the wp.blocks.createBlock API.
- Use the WordPress Block Editor Handbook as a reference for block development.
- Consider using a build tool like Webpack to bundle your block scripts.
Recommended Links:
