Build interactive blocks using WordPress Interactivity API. Covers data-wp-* directives, stores, context management, and server-side rendering patterns. Use when adding client-side interactivity to blocks without a full JavaScript framework.
Build interactive blocks using WordPress Interactivity API. Covers data-wp-* directives, stores, context management, and server-side rendering patterns. Use when adding client-side interactivity to blocks without a full JavaScript framework.
license
MIT
compatibility
WordPress 6.5+, PHP 8.0+, requires viewScriptModule support
This skill guides you through building interactive blocks using the WordPress Interactivity API, a lightweight reactive system for block-based interactivity.
When to Use
Adding click handlers, toggles, or state changes to blocks
Building accordions, tabs, modals, or carousels
Creating interactive navigation or menus
Replacing jQuery-based interactions with modern patterns
Any client-side interactivity that doesn't need a full SPA framework
Key Concepts
The Interactivity API provides:
Directives - HTML attributes that bind behavior (data-wp-*)
Stores - State management with actions and callbacks
For complex initial state, use wp_interactivity_state():
// In your block's render callback or pluginwp_interactivity_state('tenup/accordion', [
'items' => [],
'allowMultiple' => true,
]);
This merges with client-side store state.
Step 6: Handle Context Inheritance
Context flows down the DOM tree:
<divdata-wp-interactive="tenup/tabs"data-wp-context='{"activeTab":0}'><!-- Child elements can access context.activeTab --><buttondata-wp-on--click="actions.setTab"data-wp-context='{"tabIndex":0}'><!-- This element has both activeTab and tabIndex in context --></button></div>
Access nested context in actions:
actions: {
setTab() {
const context = getContext();
// context.tabIndex is from the button// context.activeTab is from the parent
context.activeTab = context.tabIndex;
}
}