Building Modern Reactive WordPress UI with the Interactivity API (No React Build Step)

, ,
WordPress Interactivity API DOM State Hydration and Reactive UI Guide

Introduced in WordPress 6.5 and refined in 6.6+, the Core Interactivity API represents the most important architectural addition to modern WordPress frontend development. It provides a standardized, ultra-lightweight reactive framework for building dynamic micro-frontends—like live search filters, modal dialogs, and instant cart drawers—directly in native HTML directives without requiring a separate React, Vue, or Webpack build setup.

The Core Philosophy: Server-Rendered HTML + Client Hydration

Traditional Single Page Applications (SPAs) ship massive JavaScript bundles that parse the entire DOM on the client side. This causes poor Largest Contentful Paint (LCP) and high Interaction to Next Paint (INP) latency on mobile devices. The WordPress Interactivity API follows an “isomorphic island” approach:

  • Zero Initial JS Delay: HTML is rendered natively on the server by PHP, ensuring 100/100 SEO indexability, perfect Open Graph scraping, and sub-millisecond initial paint.
  • Declarative Directives: HTML elements declare dynamic behavior using standard data-wp-* attributes (e.g., data-wp-on--click, data-wp-bind--hidden, data-wp-context).
  • Shared Global State: A global reactive store (powered by Preact signals under the hood) manages synchronized state across disconnected blocks on the entire page.
  • Standardized Namespace Architecture: Eliminates JavaScript naming collisions between different plugins and themes through distinct store namespaces.

Step-by-Step: Building an Instant Live Search Filter

Let’s build a fully functional, reactive live snippet search filter using pure Interactivity API directives. We will create both the server template and the client store.

1. The Server-Rendered HTML Block Template (render.php)

HTML
<div 
  data-wp-interactive="tipwp/searchFilter"
  data-wp-context='{ "query": "", "isOpen": false, "activeCount": 19 }'
  class="search-filter-wrapper"
>
  <!-- Search Input Bar -->
  <div class="input-container" style="display: flex; gap: 0.5rem; align-items: center;">
    <input 
      type="text" 
      placeholder="Search 100+ code snippets..."
      data-wp-on--input="actions.handleSearch"
      data-wp-bind--value="context.query"
      class="search-input"
      style="padding: 0.75rem 1rem; border-radius: 8px; border: 1px solid #333; background: #111; color: #fff; width: 100%;"
    />
    <button 
      data-wp-on--click="actions.clearSearch"
      data-wp-bind--hidden="!context.query"
      class="clear-btn"
      style="padding: 0.75rem 1.25rem; border-radius: 8px; background: #333; color: #fff; border: none; cursor: pointer;"
    >
      Clear
    </button>
  </div>

  <!-- Dynamic Results Status Badge -->
  <div class="status-badge" style="margin-top: 1rem; color: #a1a1aa; font-size: 0.9rem;">
    Showing <strong style="color: #488CCA;" data-wp-text="state.filteredCount">19</strong> matching snippets
  </div>
</div>

2. Registering the Reactive Client Store (view.js)

Next, define the reactive actions and derived computed state inside your block or theme’s script module:

JAVASCRIPT
import { store, getContext } from '@wordpress/interactivity';

store('tipwp/searchFilter', {
  state: {
    get filteredCount() {
      const context = getContext();
      if (!context.query) return 19;
      
      // Derived reactive computation matching DOM elements
      const cards = document.querySelectorAll('.snippet-card');
      let visible = 0;
      cards.forEach(card => {
        const title = card.getAttribute('data-title') || '';
        const matches = title.toLowerCase().includes(context.query.toLowerCase());
        card.style.display = matches ? 'block' : 'none';
        if (matches) visible++;
      });
      return visible;
    }
  },
  actions: {
    handleSearch: (event) => {
      const context = getContext();
      context.query = event.target.value;
    },
    clearSearch: () => {
      const context = getContext();
      context.query = '';
      document.querySelectorAll('.snippet-card').forEach(c => c.style.display = 'block');
    }
  },
  callbacks: {
    onInit: () => {
      console.log('TipWP Interactivity Store Initialized (0 React overhead).');
    }
  }
});

3. Enqueuing Modern Script Modules in PHP

In WordPress 6.5+, script modules are registered with wp_register_script_module() and enqueued with wp_enqueue_script_module():

PHP
<?php
function tipwp_register_interactivity_module() {
    wp_register_script_module(
        'tipwp-search-filter',
        get_template_directory_uri() . '/assets/js/search-filter.js',
        ['@wordpress/interactivity'],
        '1.0.0'
    );
    wp_enqueue_script_module('tipwp-search-filter');
}
add_action('wp_enqueue_scripts', 'tipwp_register_interactivity_module');

Key Advantages Over Heavy Frontend Frameworks

Feature / Metric WordPress Interactivity API React / Vue Frontend Island
Runtime Bundle Size ~10 KB (Shared Core Runtime) 45 KB – 130 KB per island
Build Pipeline Required None (Native Script Modules) Vite, Webpack, or Babel
Block Theme Interoperability 100% Native to Gutenberg & FSE Requires custom iframe/bridge wrappers
Interaction to Next Paint (INP) < 35ms (Instant Microtask) 120ms – 250ms (Hydration lag)
SEO & SSR Hydration Zero layout shift, 100% server-rendered Subject to client hydration flashes

Production Best Practices for Interactivity API

  1. Keep Context Localized: Use data-wp-context for component-level ephemeral state (search inputs, modal visibility, open/closed accordion panels) and reserve global state for cross-component shared state (active cart items, currency converters, theme dark mode switches).
  2. Always Ensure Progressive Enhancement: Ensure forms and links have standard HTML fallbacks so users without JavaScript or on slow 3G connections can still submit search queries or navigate pages seamlessly.
  3. Leverage Directives Instead of Event Listeners: Avoid manual addEventListener calls in your client scripts. Let the Interactivity API runtime bind events declaratively through data-wp-on--* directives for optimal memory cleanup when blocks are unmounted.

Experience the speed of native FSE architecture in action with our TipWP Pro Theme, built from the ground up on modern WordPress standards.

← Back to Snippet Index Back to top ↑