Skip to main content
Dynamic blocks build their structure and content on the fly when rendered on the frontend. Unlike static blocks, their output is generated server-side using PHP, allowing for dynamic content that updates automatically.

When to Use Dynamic Blocks

Dynamic blocks are ideal for:
  1. Auto-updating content: Content that should change even if a post hasn’t been updated (e.g., Latest Posts block)
  2. Immediate code updates: When updates to block code (HTML, CSS, JS) should appear immediately across all instances without validation errors
  3. Server-side data: Content that requires database queries or server-side processing

How Dynamic Blocks Work

For dynamic blocks:
  1. The save function typically returns null
  2. Only block attributes are saved to the database
  3. A server-side render_callback function generates the output
  4. Validation is skipped since there’s no saved markup to validate

Basic Dynamic Block

JavaScript (Block Registration)

PHP (Server-Side Rendering)

Using Block Attributes

Dynamic blocks can save attributes to customize behavior.

JavaScript with Attributes

PHP Render Callback with Attributes

Using ServerSideRender

The <ServerSideRender> component shows a live preview of the server-rendered output in the editor.
Server-side render is a fallback. Client-side rendering in JavaScript is always preferred as it’s faster and allows better editor manipulation.

Dynamic Blocks with InnerBlocks

If your dynamic block contains InnerBlocks, you must save them:

Working with Block Supports

Dynamic blocks can use Block Supports for features like colors, spacing, and typography.

JavaScript

PHP

Always use get_block_wrapper_attributes() in your render callback to ensure Block Supports are properly applied.

Common Patterns

Query-Based Block

User Data Block

Performance Considerations

  1. Cache when possible: Use transients or object caching for expensive queries
  1. Limit query results: Don’t query more data than needed
  2. Use efficient queries: Leverage WordPress query optimizations
  3. Consider pagination: For large datasets, implement pagination

Best Practices

  1. Always use get_block_wrapper_attributes(): Ensures Block Supports work correctly
  2. Escape output: Use esc_html(), esc_url(), esc_attr() appropriately
  3. Validate attributes: Check and sanitize attribute values
  4. Provide fallbacks: Handle cases where data isn’t available
  5. Use block.json: Define attributes and supports in block.json for better organization