The WordPress Interactivity API: interactive blocks without React on the front end
Blocks are built with React in the editor, but on the front end they are static HTML. For an accordion, a tab group or a “show more” button, every block author used to write their own vanilla JavaScript or ship a framework. Since WordPress 6.5 (April 2024), the Interactivity API is the standard way: the server renders the HTML with data-wp-* directives, and a small shared runtime makes it interactive.
Problem: A plugin has an FAQ block with collapsible answers. Its front-end script queries the DOM, toggles classes and ARIA attributes by hand, breaks when two FAQ blocks are on one page, and the HTML before the script runs does not match the HTML after.
Solution: Declare "interactivity": true in block.json, render the block in render.php with data-wp-interactive, a per-block data-wp-context and directives like data-wp-on--click and data-wp-bind--hidden, and put the logic into a store registered in a script module loaded with viewScriptModule.
block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "ha/faq-item",
"title": "FAQ item",
"category": "text",
"attributes": {
"question": { "type": "string", "default": "" },
"answer": { "type": "string", "default": "" }
},
"supports": { "interactivity": true },
"render": "file:./render.php",
"viewScriptModule": "file:./view.js"
}
viewScriptModule (new in 6.5) loads view.js as an ES module, only on pages that contain the block. "interactivity": true tells WordPress that the block uses directives, so the server processes them.
render.php
<?php
$ha_id = wp_unique_id( 'ha-faq-' );
?>
<div
<?php echo get_block_wrapper_attributes(); ?>
data-wp-interactive="ha/faq"
<?php echo wp_interactivity_data_wp_context( array( 'open' => false ) ); ?>
>
<h3>
<button
type="button"
aria-controls="<?php echo esc_attr( $ha_id ); ?>"
data-wp-on--click="actions.toggle"
data-wp-bind--aria-expanded="context.open"
>
<?php echo esc_html( $attributes['question'] ); ?>
</button>
</h3>
<div id="<?php echo esc_attr( $ha_id ); ?>" data-wp-bind--hidden="!context.open">
<?php echo wp_kses_post( wpautop( $attributes['answer'] ) ); ?>
</div>
</div>
data-wp-interactive="ha/faq"names the store the directives inside refer to.wp_interactivity_data_wp_context()printsdata-wp-contextwith JSON: the local state of this one block instance. Two FAQ items on the page have two independent contexts.data-wp-bind--aria-expandedanddata-wp-bind--hiddenbind attributes to state;!negates. The server already evaluates them when it renders the page, so the HTML is correct before any JavaScript runs: the answers are hidden,aria-expandedisfalse.
view.js
import { store, getContext } from '@wordpress/interactivity';
store( 'ha/faq', {
actions: {
toggle() {
const context = getContext();
context.open = ! context.open;
},
},
} );
The action changes the context, and the runtime updates every directive that depends on it. No DOM queries, no class toggling. @wordpress/interactivity is not bundled into the file: WordPress registers it as a script module and the import map in the page resolves it. With @wordpress/scripts, build with wp-scripts build --experimental-modules so that the import stays external.
Global state and derived values
Values shared by all instances go into the global state, set from PHP:
wp_interactivity_state( 'ha/faq', array(
'expandAllLabel' => __( 'Expand all', 'textdomain' ),
) );
import { store, getContext } from '@wordpress/interactivity';
const { state } = store( 'ha/faq', {
state: {
get label() {
return getContext().open ? 'Close' : state.expandAllLabel;
},
},
} );
A getter is a derived value: data-wp-text="state.label" shows it and updates when open changes. Derived state computed only in JavaScript is not known to the server; for the first render, give it the same value from PHP or keep the initial text in the HTML.
NOTE: The Interactivity API is meant for the front end of blocks; the editor still uses React. It also works in classic themes, as long as the HTML is rendered by a block or passed through wp_interactivity_process_directives(). For blocks in general and their block.json: block.json explained.