How to Build Interactive WordPress Blocks with the Interactivity API
WordPress blocks can do more than display static content; the Interactivity API lets them respond to what visitors do without a full page reload.
That makes interactive blocks useful for both site visitors, who can reveal or change content in place, and the people building WordPress sites, who need those interactions to work reliably.
Building a block with the Interactivity API means connecting its controls and visible state in a way that WordPress can manage.
The Interactivity API is planned for WordPress 6.5. This article introduces the approach using a test installation of the upcoming release; see the WordPress developer note for setup details.
Understand the WordPress Interactivity API
The WordPress Interactivity API lets you add features like menus, filters, and live updates to blocks without reloading the page. You can use it to connect what visitors do with how your page responds. For example, a click can reveal content, or a selection can update results.
Understand the basics
The Interactivity API fundamentals include shared state, actions, and directives. State holds information, such as whether a menu is open. Actions change that information, and directives connect it to page elements. This structure helps you keep behavior clear and easier to maintain.
State stores information, actions update it, and directives connect it to page elements for clear, maintainable interactions.
Think about common WordPress interaction patterns before you build. Decide what a visitor will do, what should change, and whether the change needs to appear right away.
Keep each interaction focused, provide clear feedback, and consider keyboard access. These choices help visitors understand your block and use it without confusion.
Create a Block for an Interactive Toggle
Start by defining the toggle’s state, such as whether its content is open or closed.
Add Interactivity API directives to connect that state to the block’s button and content, then test both states on the front end.
Define Toggle State
A toggle needs a clear starting state so it knows whether to show or hide its content. In your block’s markup, initialize a local data-wp-context with an isOpen value of false so the content starts hidden. Register the action in a store using the same namespace as data-wp-interactive.
Keep this value simple, and use a Boolean rather than text such as “open” or “closed.”
- Choose a clear name, such as
isOpen, that describes what the value tracks. - Set the default to
falsewhen visitors should first see the content hidden. - Use
trueas the starting value if your design should show the content immediately. - Keep each toggle’s state separate, so opening one doesn’t affect another.
Check the block’s intended starting appearance before choosing the default. This state gives your toggle a reliable value to work with as you build its behavior.
Add Interactivity Directives
With isOpen set to its starting value, add Interactivity API directives to connect that state to the toggle’s markup. Put the directives on the relevant HTML elements in your block’s saved markup.
| Element | Directive | Purpose |
|---|---|---|
| Button | data-wp-text |
Sets its label |
Use data-wp-interactive on the wrapper to identify your namespace, such as my-plugin/toggle. Then bind the button’s click event to your store action, using the Directive attribute syntax exactly, including the double hyphen before the event name. Keep event binding patterns consistent with your existing store.
Connect the panel’s hidden attribute to !context.isOpen, and the button’s expanded value to context.isOpen. These bindings keep markup tied to state, without adding separate DOM updates.
Test Block Behavior
How can you tell whether the toggle works? Check its editor representation and test the interaction on the front end, then confirm the Interactivity API updates the right state. Start with a clean page, and check both mouse and keyboard use.
- Click the toggle, verify the content appears, then click again to hide it.
- Press Tab to reach the control, then press Enter or Space to switch it.
- Reload the page and confirm the initial state matches your design.
- Resize the browser and test on a phone, checking that labels stay readable.
Add automated toggle checks with a browser test, such as Playwright, to verify state changes consistently. Include interaction edge cases: rapid clicks, missing content, and multiple toggle blocks on one page. Check the console for errors, and fix each issue before you publish.
Add Interactivity Directives to Your Block
Once your block has a view script and a shared context, add Interactivity API directives to its markup to connect user actions with that state.
Add Interactivity API directives to your block’s markup to connect user actions with its view script and shared state.
Add directives to your markup
- Open your block’s
render.phpfile, then add thedata-wp-interactiveattribute to its root element. Set its value to your namespace, such asmy-plugin/menu. - Add directives to the elements that need behavior. For example, use
data-wp-bind--hiddento connect an element’s visibility to a context value, ordata-wp-on--clickto name a click handler. - Use
data-wp-contextwhen an element needs its own context values. Keep the JSON valid, and match property names to those used in your view script.
Follow the Directive syntax exactly, including the double hyphen in directive names. Choose semantic elements and accessible controls, such as buttons for actions, so people can use your block with a keyboard.
Confirm labels describe each control’s purpose.
Manage Toggle State and Actions
You can keep a toggle’s state in sync across the elements it controls.
Define actions to change that state when someone clicks or activates a control.
Toggle State Across Elements
When several parts of a block respond to the same interaction, keep their state in sync with a shared context value. With shared state, you can update a menu, its icon, and a related message together.
This keeps synchronized toggles from drifting out of step.
- Store the open or closed value in the block’s context, rather than creating separate values for each element.
- Let each element read that value to decide what to show, hide, or label.
- Change the value from the control the visitor uses, so every connected element reflects the same choice.
- Check both states on the front end, including narrow screens and keyboard use.
Keep context scoped to the block instance. If your page has two independent menus, each needs its own value, so opening one doesn’t affect the other.
Define Interactive Actions
Define an action for the control that changes the local context value, then connect it to the button. The following is an action fragment: import store and getContext from @wordpress/interactivity, register it within store( 'my-plugin/toggle', { ... } ) in the block’s view script module, and use that same namespace and an initial data-wp-context in the markup:
actions: {
toggle() {
const context = getContext();
context.isOpen = !context.isOpen;
},
},This action flips the current value, so the same control opens and closes the panel. Add an event handler in your markup to call it when someone activates the button:
<button data-wp-on--click="actions.toggle">Toggle panel</button>Use the same context key in your visibility bindings, so the panel responds to each change. Keep action definitions focused on one task, and check that the property starts with a clear default value.
If several controls share the state, connect each to the same action rather than duplicating toggle logic. Test clicks and keyboard activation.
Test Your Block in the Editor and Front End
Check your block’s editor representation and test its behavior on the front end. The Interactivity API powers the visitor-facing markup; an interactive editor preview requires its own editor implementation. Preview the page before publication. A careful test helps you confirm that your defined actions behave as expected.
- Check the editor: Add the block to a page and verify its content and layout. An interactive editor preview needs its own implementation.
- Check different inputs: Try buttons, links, and keyboard controls, so you know people can use the interaction in more than one way.
- Frontend verification: Open the page in a private browser window, then repeat the same actions without editor controls.
- Compare results: Check the block’s layout in both views and verify the front-end state after reloading the page.
Record what works before moving on.
Fix Common WordPress Interactivity Issues
If your block stops responding, check its state, directive names, and browser console before changing the code. A misspelled directive can prevent an action from running; for a click handler, the correct syntax is data-wp-on--click. Confirm each directive matches the Interactivity API syntax and that your script loads on the page.
Debugging directive errors
Open your browser’s developer tools and review the console for errors. Check that your store uses the correct namespace and that each action or callback is exported as expected.
Test one interaction at a time, so you can identify which change causes the problem.
Resolving state conflicts
If two blocks update the same value, give each store a clear namespace or keep shared data in one store. Check initial state on both the server and client, since mismatches can cause unexpected updates.
Then test again in the editor and front end.
Frequently Asked Questions
Which WordPress Versions Support the Interactivity API?
The stable Interactivity API is planned for WordPress 6.5. Before that release, use a test installation of the 6.5 prerelease or consult the Gutenberg project’s experimental API documentation.
Can the Interactivity API Work With Classic Themes?
Yes, interactive blocks can work with classic themes when the required assets are loaded. Classic theme compatibility depends on loading the necessary scripts, while PHP rendering can output interactive blocks that work seamlessly.
How Does the Interactivity API Affect Page Performance?
The Interactivity API can improve page performance when you use it thoughtfully. Asset loading impact stays limited through on-demand scripts, while runtime efficiency benefits from targeted updates. Still, complex interactions can increase JavaScript costs and slow pages.
Can Multiple Interactive Blocks Share State?
Yes, you can share state across interactive blocks by using the same store namespace. Shared block state lets you coordinate behavior; Namespace coordination helps prevent conflicts, so choose consistent keys and manage updates carefully as blocks interact.
What Accessibility Standards Should Interactive Blocks Meet?
Your interactive blocks should meet WCAG 2.2 AA: provide Keyboard navigation, screen reader support, visible focus, sufficient contrast, clear labels, and accessible error messages. You should also manage focus appropriately and communicate dynamic updates without disrupting users.
- Interactive Blocks
- WordPress development
- WordPress Interactivity API
About the author
Vladimir Mrkela
Senior WordPress Developer
I’m a senior WordPress developer and co-founder of Pixel Industry. I’ve spent more than 12 years building websites, custom plugins, and integrations for all kinds of projects. I enjoy working on complex architecture, solving tricky problems, and sharing what I learn along the way.