Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block with dynamic content in WordPress?
Asked on Dec 12, 2025
Answer
Creating a custom Gutenberg block with dynamic content in WordPress involves using the Block API and PHP to render content dynamically. This allows you to display data that can change based on conditions or user input.
<!-- BEGIN COPY / PASTE -->
// JavaScript: Register the block
wp.blocks.registerBlockType('myplugin/dynamic-block', {
title: 'Dynamic Block',
category: 'widgets',
edit: function() {
return wp.element.createElement('p', {}, 'Dynamic content will appear here.');
},
save: function() {
return null; // Server-side rendering
}
});
// PHP: Render the block dynamically
function myplugin_render_dynamic_block($attributes) {
$dynamic_content = 'This is dynamic content generated by PHP.';
return '<p>' . esc_html($dynamic_content) . '</p>';
}
register_block_type('myplugin/dynamic-block', array(
'render_callback' => 'myplugin_render_dynamic_block',
));
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have the necessary JavaScript and PHP files set up in your WordPress plugin or theme.
- Use the "wp_enqueue_script" function to load your block's JavaScript file.
- Dynamic blocks are rendered on the server side using a PHP callback function.
- Test your block to ensure it displays the dynamic content as expected.
Recommended Links:
