Tabs, accordions, filters, “load more” buttons, mini carts: almost every modern website needs some interactivity. For years, WordPress developers solved this in many different ways, from jQuery snippets to full React apps. Each approach worked, but blocks from different plugins rarely shared state or followed the same patterns. The WordPress Interactivity API brings a common standard to this part of WordPress development.
In this guide I explain what the Interactivity API is, why it was created, and the core ideas you need to understand. Then we build a real interactive FAQ block together, step by step, and finish with best practices for performance and accessibility. If you have built a simple block before, you will be comfortable with everything here.
What Is the WordPress Interactivity API?
The Interactivity API is a standard system for adding front-end interactions to blocks. It was merged into WordPress core in version 6.5 and is provided by the @wordpress/interactivity package, which ships with WordPress itself. You do not need to install it separately inside WordPress.
It works with two simple ingredients:
- Directives: special HTML attributes, such as
data-wp-on--click, that you add to your block’s markup to describe behaviour. - Stores: small JavaScript objects that hold state and the actions that change it.
Instead of writing code that finds an element and manually changes it, you declare how the HTML should look for a given state. When the state changes, the API updates the page for you. This declarative and reactive approach will feel familiar if you have used modern JavaScript frameworks, but it is designed specifically around how WordPress renders pages on the server.
Why Use the Interactivity API?
- Server-side rendering first: WordPress processes directives in PHP, so the initial HTML already reflects the correct state. Visitors and search engines see real content immediately.
- Small and fast: the runtime is lightweight and loads as a script module, which is good for Core Web Vitals.
- Blocks can talk to each other: blocks that share a store namespace can share state. An “Add to cart” button and a mini cart counter can stay in sync without custom events.
- Consistency: every developer uses the same patterns, which makes code easier to read, review and maintain.
- Client-side navigation: with the router, pages can update parts of the screen without a full reload, as enhanced pagination in the Query Loop block shows.
Where you already see it
You have probably used the Interactivity API without noticing. Several core blocks rely on it, including the Image block’s “expand on click” lightbox, the expandable Search block, the Navigation block’s overlay menu, and the Query Loop’s option to paginate without a full page reload. That is a strong sign the API is mature and ready for production work.

Core Concepts You Need to Know
Namespaces
The data-wp-interactive directive activates the API for an element and its children and sets the store namespace, for example my-faq. Directives inside that element refer to state and actions from that namespace.
Global state
State lives in the store and is shared by every element that uses the same namespace. You can set initial state on the server with wp_interactivity_state(), so the HTML is correct before any JavaScript runs.
Local context
Context is data that belongs to one element and its children, defined with data-wp-context. It is perfect when you have several copies of the same block on a page, such as multiple FAQ items, and each needs its own open or closed status.
Actions and callbacks
Actions are functions that change state, usually in response to events like clicks. Callbacks run as side effects, for example when an element is initialised or when state they depend on changes.
Derived state
Derived state uses JavaScript getters to compute values from other state, such as “has items” from a cart count. It keeps your logic in one place and your markup simple.
The Directives You Will Use Most
data-wp-interactive: activates interactivity and sets the namespace.data-wp-context: defines local context as JSON.data-wp-on--click(or any event): runs an action when the event fires.data-wp-bind--hidden(or any attribute): binds an HTML attribute to a value.data-wp-class--is-open: adds or removes a class.data-wp-text: sets the text content of an element.data-wp-style--color: sets an inline style property.data-wp-initanddata-wp-watch: run callbacks when an element is created or when its dependencies change.data-wp-each: renders a list from an array.
The full list, with examples, is in the official Interactivity API reference.
Tutorial: Build an Interactive FAQ Block
Let us build a simple, accessible FAQ item that opens and closes when clicked. Each FAQ on the page manages its own state through local context.
Step 1: Scaffold the block
The fastest way to start is the official interactive template for @wordpress/create-block. Run this in your wp-content/plugins folder:
npx @wordpress/create-block@latest my-faq --template @wordpress/create-block-interactive-template
This creates a plugin with a block that already has interactivity enabled, a PHP render file and a view script module. Activate the plugin in your dashboard. If you want a disposable environment for experiments like this, my guide to WordPress Playground and local development shows how to spin one up in seconds.
Step 2: Check block.json
Two settings matter. The block must declare interactivity support, and the front-end script is loaded as a module:
{
"supports": {
"interactivity": true
},
"render": "file:./render.php",
"viewScriptModule": "file:./view.js"
}
Step 3: Write the markup in render.php
Here we add directives to the server-rendered HTML. The wp_interactivity_data_wp_context() helper safely prints the context attribute for us.
<?php
$panel_id = wp_unique_id( 'faq-panel-' );
$context = array( 'isOpen' => false );
?>
<div
<?php echo get_block_wrapper_attributes(); ?>
data-wp-interactive="my-faq"
<?php echo wp_interactivity_data_wp_context( $context ); ?>
>
<button
type="button"
aria-controls="<?php echo esc_attr( $panel_id ); ?>"
data-wp-on--click="actions.toggle"
data-wp-bind--aria-expanded="context.isOpen"
>
<?php echo esc_html( $attributes['question'] ?? 'Question' ); ?>
</button>
<div
id="<?php echo esc_attr( $panel_id ); ?>"
data-wp-bind--hidden="!context.isOpen"
>
<?php echo wp_kses_post( $attributes['answer'] ?? '' ); ?>
</div>
</div>
Notice that the answer panel starts hidden, because the server processes data-wp-bind--hidden using the initial context. Visitors never see a flash of open content, and the markup is already accessible thanks to aria-expanded and aria-controls.
Step 4: Write the store in view.js
import { store, getContext } from '@wordpress/interactivity';
store( 'my-faq', {
actions: {
toggle() {
const context = getContext();
context.isOpen = ! context.isOpen;
},
},
} );
That is the whole interaction. The action flips isOpen in the local context, and the API automatically updates aria-expanded on the button and the hidden attribute on the panel. No query selectors, no manual DOM updates.
Step 5: Build and test
Run npm run build (or npm start while developing), add a few FAQ blocks to a page, and test them with both a mouse and a keyboard. Each item opens independently because each has its own context.

Going Further: Global State and Derived Values
Local context is ideal for independent items. When several blocks need the same data, use global state. Here is a small example for a cart counter shared by different blocks. First, set the initial state on the server so the HTML is correct on the first render:
wp_interactivity_state( 'my-shop', array(
'count' => 0,
) );
Then define actions and a derived value in JavaScript:
import { store } from '@wordpress/interactivity';
const { state } = store( 'my-shop', {
state: {
get hasItems() {
return state.count > 0;
},
},
actions: {
addToCart() {
state.count += 1;
},
},
} );
Any block in the my-shop namespace can now use data-wp-text="state.count" to show the number or data-wp-class--is-visible="state.hasItems" to reveal a badge. When one block calls the action, every block that reads the state updates together.
Handling events that need preventDefault
Actions run asynchronously by default, which is good for performance. If an action needs synchronous access to the event, for example to call event.preventDefault() on a form submit, wrap it with the withSyncEvent() helper introduced in WordPress 6.8. This keeps the default fast path for most actions while supporting the cases that truly need it.
Client-side navigation with the router
The @wordpress/interactivity-router module lets a region of the page update when the URL changes, without a full reload. Core uses it for the Query Loop’s enhanced pagination. It is a powerful tool for search results, filters and archive pages, and it is worth exploring once you are comfortable with the basics.
How It Compares to Other Approaches
Every tool has its place, and many excellent sites use jQuery, vanilla JavaScript, Alpine.js or React. The Interactivity API simply offers a WordPress-native option with some clear strengths:
- Compared with vanilla JavaScript or jQuery: less manual DOM code, built-in reactivity and server-rendered initial state.
- Compared with a client-rendered React app: content is in the HTML from the start, and the front-end payload stays small. React remains a great choice for complex app-like admin interfaces.
- Compared with other lightweight libraries: the concepts are similar, but the Interactivity API is integrated with block rendering, script modules and core blocks.
Best Practices for Interactive Blocks
Accessibility
- Use real
buttonelements for things people click, so keyboard and screen reader support works automatically. - Bind ARIA attributes such as
aria-expandedto state so assistive technology always gets the current status. - Manage focus thoughtfully when opening dialogs or menus. My WordPress accessibility checklist covers focus and keyboard testing in detail.
Performance
- Keep actions short and give instant visual feedback. This directly helps Interaction to Next Paint, as I explain in my WordPress INP optimization guide.
- Set initial state on the server, so there is no layout shift when JavaScript loads.
- Keep context small. Store IDs and flags rather than large data structures.
Code organisation
- Use a unique namespace per plugin, and share a namespace only when blocks intentionally share state.
- Prefer derived state over duplicated values.
- Escape everything you print in PHP, as with any block render file.
Debugging Tips
- Nothing happens on click: check that
supports.interactivityis true, that the view script module is loading, and that the namespace indata-wp-interactivematches the one instore(). - Initial HTML looks wrong: confirm you set initial state with
wp_interactivity_state()or context in PHP, so server directive processing has the values it needs. - Context not updating: make sure you read context with
getContext()inside the action, not outside it. - Enable debugging: turn on
SCRIPT_DEBUGin a development environment to get clearer warnings in the browser console.
Where the API Is Heading
The Interactivity API continues to evolve with each release. The roadmap for WordPress 7.2 lists a further iteration of the API, alongside design improvements such as responsive style states that build on what I covered in responsive styling without CSS. For the bigger picture of recent releases, see my WordPress 7.1 features guide.
Real-World Ideas for Interactive Blocks
Once the FAQ example clicks, you will start seeing opportunities everywhere. Here are practical features I like to build with the Interactivity API, from simple to more advanced:
Tabs and content switchers
Store the active tab index in context, bind aria-selected on each tab button and hidden on each panel. Add arrow-key navigation in an action so the component follows the familiar tab pattern that keyboard and screen reader users expect.
Product or post filters
Combine filter buttons with the router so the URL updates and only the results region refreshes. Visitors get a fast, app-like experience, and every filtered view still has a shareable URL that search engines can crawl.
Load more buttons
A “Load more” button can fetch the next page of posts and append them, while a status message announces how many items were added. Keep the regular pagination links in the HTML as a fallback, so the page still works if JavaScript fails to load.
Mini cart and wishlist counters
This is where shared global state shines. An add-to-cart button in a product grid and a counter in the header can read the same state, so both update instantly together without custom events or extra libraries.
Reading progress and table of contents highlights
Use a data-wp-init callback to set up an observer, store the current section in state, and bind an active class to the matching link. Long articles become easier to navigate, and the logic stays small and readable.
Whatever you build, start with the HTML that works without JavaScript, then layer interactivity on top. This progressive enhancement mindset is exactly what the API was designed for, and it keeps your blocks robust, accessible and fast.
Frequently Asked Questions
Do I need React to use the Interactivity API?
No. You write plain HTML directives and a small JavaScript store. React is still used inside the block editor, but the front-end interactivity does not depend on it.
Can I use it in a classic theme?
The API is designed for blocks, and blocks work in classic themes too. Directives are processed for block output, so building your interactive feature as a block is the recommended path.
Is it good for SEO?
Yes. Because directives are processed on the server, the initial HTML contains your real content and state, which search engines can read without running JavaScript.
Which WordPress version do I need?
WordPress 6.5 or newer includes the API. For the latest features and fixes, use the current major release.
Conclusion
The WordPress Interactivity API gives developers a clean, shared way to build interactive blocks that are fast, accessible and friendly to search engines. You describe behaviour with directives, keep logic in a small store, and let WordPress render the correct HTML on the server. Start with a simple toggle like the FAQ block above, then explore global state, derived values and the router.
For deeper learning, work through the official Interactivity API quick start guide and the original merge announcement, which explains the design goals behind the API.

Leave a Reply