Skip to content

Latest commit

 

History

History

block-dynamic-rendering-64756b

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Dynamic Block Rendering Implementation

This example demonstrates how to create WordPress blocks that render their content dynamically on the server side. Unlike static blocks, dynamic blocks generate their output at runtime, making them ideal for content that needs to be updated based on the latest data or server-side logic.

Key concepts covered:

  • Dynamic block rendering
  • Server-side PHP rendering
  • Block attribute handling
  • WordPress render callback
  • Real-time content generation
Example Description Tags Download .zip Live Demo
Basic Block with Dynamic Rendering Demonstrates how to create a block that renders its content dynamically on the server side rather than saving static content in the post content. dynamic-rendering create-block 📦

Understanding the Example Code

Dynamic Rendering Implementation

  1. Block Registration

    import { registerBlockType } from '@wordpress/blocks';
    import { useBlockProps } from '@wordpress/block-editor';
    
    registerBlockType( 'block-development-examples/dynamic-rendering', {
    	edit: function Edit( { attributes, setAttributes } ) {
    		const blockProps = useBlockProps();
    		return (
    			<div { ...blockProps }>
    				<p>Editor View: Content will be rendered dynamically</p>
    			</div>
    		);
    	},
    	// No save function needed - rendering handled by PHP
    	save: function () {
    		return null;
    	},
    } );
  2. PHP Render Callback

Render Callback is defined via a render.php file as defined on block.json

Technical Components

  1. Block Configuration

    {
    	"apiVersion": 2,
    	"name": "block-development-examples/dynamic-rendering",
    	"title": "Dynamic Rendering Block",
    	"category": "widgets",
    	"icon": "update",
    	"description": "Block with dynamic server-side rendering.",
    	"supports": {
    		"html": false
    	},
    	"textdomain": "dynamic-rendering",
    	"editorScript": "file:./index.js",
    	"render": "file:./render.php"
    }
  2. Block Registration Process

function block_dynamic_rendering_64756b___register_block() {
	register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'block_dynamic_rendering_64756b___register_block' );

Related Resources


Note Check the Start Guide for local development with the examples