Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How do I create a custom Gutenberg block with dynamic content?
Asked on Feb 20, 2026
Answer
Creating a custom Gutenberg block with dynamic content involves registering a block in WordPress and using PHP to render dynamic content on the server side. This process requires both JavaScript for the block editor and PHP for the dynamic content.
<!-- BEGIN COPY / PASTE -->
// JavaScript: Register the block
wp.blocks.registerBlockType('myplugin/dynamic-block', {
title: 'Dynamic Block',
icon: 'smiley',
category: 'widgets',
edit: function() {
return wp.element.createElement('p', null, 'Dynamic Block Editor');
},
save: function() {
return null; // Server-side rendering
}
});
// PHP: Server-side rendering
function myplugin_dynamic_block_render_callback() {
return '<p>' . esc_html( 'This is dynamic content!' ) . '</p>';
}
register_block_type('myplugin/dynamic-block', array(
'render_callback' => 'myplugin_dynamic_block_render_callback',
));
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you enqueue the JavaScript file for the block using "wp_enqueue_script" in your plugin or theme.
- Use "register_block_type" to define the block and its server-side rendering callback in PHP.
- Dynamic blocks are useful for content that changes often or needs to be generated based on conditions.
- Consider using the WordPress REST API if you need to fetch data from external sources.
Recommended Links:
