block.json in WordPress: apiVersion, attributes, supports and registration
Since WordPress 5.8 a block can be described completely in a block.json file. It is the single source of truth for the block: name, title, category, icon, attributes, supported features, scripts and styles. WordPress reads it in PHP with register_block_type() and in JavaScript, and enqueues the assets itself.
Problem: A block plugin registers all blocks in one big JavaScript file, and a new block means changes in three places: PHP, the registration script and the build config. The goal is a folder per block, where a new block is just a new folder with a block.json.
Solution: Write a block.json for every block and register each folder with register_block_type( $folder ). WordPress reads the metadata and handles the enqueueing.
The block.json file
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "my-plugin/callout",
"version": "1.0.0",
"title": "Callout",
"category": "text",
"icon": "megaphone",
"description": "A highlighted callout box with a message.",
"keywords": [ "callout", "notice", "alert" ],
"textdomain": "my-plugin",
"attributes": {
"message": {
"type": "string",
"default": ""
},
"variant": {
"type": "string",
"enum": [ "info", "warning", "success", "error" ],
"default": "info"
}
},
"supports": {
"align": [ "wide", "full" ],
"html": false,
"color": { "text": true, "background": true },
"typography": { "fontSize": true },
"spacing": { "padding": true }
},
"example": {
"attributes": { "message": "This is an important notice for your readers.", "variant": "info" }
},
"editorScript": "file:./index.js",
"editorStyle": "file:./editor.css",
"style": "file:./style.css"
}
What the keys do:
apiVersion: 2(WordPress 5.6+) makes the editor use the block wrapper fromuseBlockProps(), so supports like color and spacing are applied to your markup.attributesdescribe the data of the block. Without asource, the value is stored in the block comment in the post content, which is what a server-rendered block needs.- Do not name an attribute
styleorclassName: the editor uses them for the supports and the custom CSS class. supportsswitches on the standard controls;alignadds its attribute itself.examplegives the preview in the block inserter.file:./index.jsis a path relative toblock.json; WordPress turns it into a URL and takes the dependencies and the version from index.asset.php, whichwp-scripts buildgenerates.
Register the blocks in PHP
<?php
add_action( 'init', function () {
foreach ( glob( __DIR__ . '/blocks/*/block.json' ) as $block_json ) {
$dir = dirname( $block_json );
$render = $dir . '/render.php';
register_block_type( $dir, file_exists( $render ) ? array(
'render_callback' => function ( $attributes, $content, $block ) use ( $render ) {
ob_start();
include $render;
return ob_get_clean();
},
) : array() );
}
} );
Every folder with a block.json becomes a block. If the folder also has a render.php, the block is rendered on the server:
<?php
// blocks/callout/render.php: $attributes, $content and $block are available
?>
<div <?php echo get_block_wrapper_attributes( array( 'class' => 'callout callout--' . esc_attr( $attributes['variant'] ) ) ); ?>>
<p class="callout__body"><?php echo wp_kses_post( $attributes['message'] ); ?></p>
</div>
get_block_wrapper_attributes() adds the classes and inline styles of the supports (alignment, colors, padding), so they work on the front end too.
Update (2022): Since WordPress 6.1, block.json has a "render": "file:./render.php" key that does the same as the callback above: register_block_type( $dir ) is then enough.