Key points
- A dynamic block is rendered by PHP on every view instead of being frozen into the content.
- Since WordPress 6.1, the “render” key in block.json points to a plain PHP file.
- Changing the markup no longer triggers “This block contains unexpected or invalid content”.
When we take over an existing site, we often find custom blocks whose HTML is saved inside every post. The day the design changes, every page has to be reopened, and the editor complains that the block “contains unexpected or invalid content”. For anything driven by data (latest projects, pricing, team members), a dynamic block is the right tool.
Static or dynamic?
A static block writes its HTML into post_content through its JavaScript save() function. A dynamic block stores only its settings; PHP produces the HTML when the page is viewed. If the output must stay current, or the markup is likely to evolve, go dynamic.
The test we use with clients: if an editor would expect the block to update by itself when they publish a new project, it has to be dynamic. If it is copy written once, such as a quote or a call-out, a static block is fine.
block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "wts/latest-projects",
"title": "Latest projects",
"category": "widgets",
"attributes": {
"count": { "type": "number", "default": 3 }
},
"editorScript": "file:./index.js",
"render": "file:./render.php"
}
Registering it from the build folder takes one line of PHP:
add_action('init', function () {
register_block_type(__DIR__ . '/build/latest-projects');
});
render.php
The file automatically gets $attributes, $content and $block. Do not skip get_block_wrapper_attributes(): without it, the classes, colours and spacing set in the editor never reach the front end.
<?php
$projects = get_posts([
'post_type' => 'project',
'numberposts' => (int) ($attributes['count'] ?? 3),
]);
if (!$projects) return;
?>
<ul <?php echo get_block_wrapper_attributes(); ?>>
<?php foreach ($projects as $p) : ?>
<li><a href="<?php echo esc_url(get_permalink($p)); ?>">
<?php echo esc_html(get_the_title($p)); ?>
</a></li>
<?php endforeach; ?>
</ul>
In JavaScript, save returns null, and the editor can show an accurate preview through the ServerSideRender component.
Lessons from real projects
- Escape everything with
esc_html(),esc_url()andesc_attr(). The editor does not sanitise your PHP output. - Watch the queries: a block dropped into a footer template part runs on every page. A short-lived transient usually takes care of heavy queries.
- Build with @wordpress/scripts: it generates the
index.asset.phpfile that lists the editor script’s dependencies.
FAQ
Can I convert an existing static block?
Yes, but add a deprecated entry for the old markup, or the editor will flag every block already saved as invalid.
Is a dynamic block slower?
It runs PHP on each view, but with page caching or a transient visitors will not notice the difference.
Want custom blocks your team can use without breaking the layout? Building them is a big part of our WordPress development work.



