Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block for my theme?
Asked on Dec 10, 2025
Answer
Creating a custom Gutenberg block involves using JavaScript and PHP to define the block's functionality and appearance. You'll need to enqueue the necessary scripts and register the block using WordPress functions.
<!-- BEGIN COPY / PASTE -->
// Enqueue block scripts
function my_custom_block_enqueue() {
wp_enqueue_script(
'my-custom-block',
get_template_directory_uri() . '/js/my-custom-block.js',
array('wp-blocks', 'wp-editor', 'wp-element', 'wp-components')
);
}
add_action('enqueue_block_editor_assets', 'my_custom_block_enqueue');
// Register block
wp.blocks.registerBlockType('mytheme/my-custom-block', {
title: 'My Custom Block',
icon: 'smiley',
category: 'common',
edit: function(props) {
return wp.element.createElement('p', {}, 'Hello from the editor!');
},
save: function(props) {
return wp.element.createElement('p', {}, 'Hello from the saved content!');
}
});
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have Node.js and npm installed to manage JavaScript dependencies.
- Use the WordPress block editor package libraries like "wp-blocks" and "wp-element" for block development.
- Place the JavaScript file in your theme's directory, typically under a "js" folder.
- Consider using a build tool like Webpack or Babel for more complex block development.
Recommended Links:
