Ask any question about WordPress here... and get an instant response.
How can I create a custom Gutenberg block with dynamic content?
Asked on Dec 07, 2025
Answer
Creating a custom Gutenberg block with dynamic content involves using the WordPress Block API and server-side rendering. This allows you to display content that changes based on certain conditions or data sources.
<!-- BEGIN COPY / PASTE -->
// Register a dynamic block
function my_dynamic_block_register() {
register_block_type('my-plugin/dynamic-block', array(
'render_callback' => 'my_dynamic_block_render',
));
}
add_action('init', 'my_dynamic_block_register');
// Server-side rendering function
function my_dynamic_block_render($attributes) {
$dynamic_content = get_option('my_dynamic_content_option');
return '<div class="my-dynamic-block">' . esc_html($dynamic_content) . '</div>';
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you have a JavaScript file to register the block in the editor using `wp.blocks.registerBlockType`.
- Use `wp_enqueue_script` to include your block's JavaScript file in the WordPress admin area.
- Dynamic blocks are rendered on the server side, so they can access WordPress data and functions.
- Consider using the WordPress REST API for more complex dynamic content needs.
Recommended Links:
