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 16, 2025
Answer
Creating a custom Gutenberg block with dynamic content involves using WordPress's block editor API and server-side rendering. This allows you to display content that can change based on various conditions or inputs.
<!-- BEGIN COPY / PASTE -->
// Register a dynamic block
function my_dynamic_block_init() {
register_block_type('my-plugin/dynamic-block', array(
'render_callback' => 'my_dynamic_block_render',
));
}
add_action('init', 'my_dynamic_block_init');
// Render callback function
function my_dynamic_block_render($attributes) {
// Fetch dynamic content, e.g., posts, from the database
$recent_posts = wp_get_recent_posts(array('numberposts' => 5));
$output = '<ul>';
foreach ($recent_posts as $post) {
$output .= '<li>' . esc_html($post['post_title']) . '</li>';
}
$output .= '</ul>';
return $output;
}
<!-- END COPY / PASTE -->Additional Comment:
- Ensure your block is registered with a unique name, such as "my-plugin/dynamic-block".
- Use the "render_callback" to define how the block's content is generated on the server side.
- Dynamic blocks are useful for content that changes frequently, like recent posts or user-specific data.
- Consider using JavaScript for the block editor interface and PHP for server-side content rendering.
Recommended Links:
