Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I create a custom Gutenberg block with dynamic content?
Asked on Apr 08, 2026
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 can change based on certain conditions or data from the database.
<!-- BEGIN COPY / PASTE -->
// Register the block in a PHP file (e.g., my-custom-block.php)
function my_custom_block_register() {
register_block_type('my-plugin/my-custom-block', array(
'render_callback' => 'my_custom_block_render',
));
}
add_action('init', 'my_custom_block_register');
// Define the render callback function
function my_custom_block_render($attributes) {
$dynamic_content = get_some_dynamic_data(); // Fetch dynamic data
return '<div class="my-custom-block">' . esc_html($dynamic_content) . '</div>';
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure you enqueue the block's JavaScript and CSS files using "wp_enqueue_script" and "wp_enqueue_style" functions.
- Use "register_block_type" to define the block and specify a "render_callback" for server-side rendering.
- In the render callback function, fetch and return the dynamic content you want to display.
- Consider using WordPress functions like "get_posts" or "get_option" to retrieve the dynamic data.
Recommended Links:
