Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block for my WordPress site?
Asked on Dec 11, 2025
Answer
Creating a custom Gutenberg block for your WordPress site involves using JavaScript to define the block's behavior and PHP to register it with WordPress. This process allows you to add custom content elements to the block editor.
<!-- BEGIN COPY / PASTE -->
// JavaScript: Create a new block in a file like block.js
wp.blocks.registerBlockType('myplugin/custom-block', {
title: 'Custom Block',
icon: 'smiley',
category: 'common',
edit: function() {
return wp.element.createElement('p', null, 'Hello, World!');
},
save: function() {
return wp.element.createElement('p', null, 'Hello, World!');
},
});
// PHP: Register the block in your plugin or theme's functions.php
function myplugin_register_custom_block() {
wp_register_script(
'myplugin-custom-block',
plugins_url('block.js', __FILE__),
array('wp-blocks', 'wp-element')
);
register_block_type('myplugin/custom-block', array(
'editor_script' => 'myplugin-custom-block',
));
}
add_action('init', 'myplugin_register_custom_block');
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you enqueue the block's JavaScript file correctly in your theme or plugin.
- Use npm and tools like Webpack or Babel for more complex block development.
- Test your block thoroughly to ensure it works as expected in the Gutenberg editor.
- Consider using the WordPress Block Editor Handbook for more detailed guidance.
Recommended Links:
