Wp interactivity api

From Lonsdale201/wp-agent-skills · View interactive version

Wp interactivity api is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Build or audit interactive WordPress blocks with the Interactivity API: block.json interactivity support, viewScriptModule, PHP state/config/context helpers, data-wp-* directives, @wordpress/interactivity stores, hydration, async actions, and WordPress 7.1 binding rules. Use for reactive frontend bl

How to use this skill

Skill file

WordPress Interactivity API

Use WordPress's block-oriented reactive runtime when frontend elements need shared state, declarative DOM updates, or server-rendered markup that hydrates without changing. Do not use it merely to enqueue an unrelated JavaScript widget.

Choose the right mechanism

| Requirement | Prefer |

|---|---|

| One isolated click handler with no block integration | A small viewScript or viewScriptModule |

| Reactive state, directives, or communication between blocks | Interactivity API |

| Editor inspector/sidebar UI | @wordpress/data and Block Editor packages |

| Data persistence or privileged work | REST API with authorization; Interactivity API is only the UI/runtime layer |

Minimal block contract

Declare support and load a script module through block metadata:

{
  "apiVersion": 3,
  "name": "acme/counter",
  "supports": { "interactivity": true },
  "render": "file:./render.php",
  "viewScriptModule": "file:./view.js"
}

In render.php, initialize public state and emit directives safely:

<?php
wp_interactivity_state(
    'acme/counter',
    array( 'total' => 0 )
);

$context = array( 'count' => (int) ( $attributes['start'] ?? 0 ) );
?>
<div
    data-wp-interactive="acme/counter"
    <?php echo wp_interactivity_data_wp_context( $context ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Core returns a complete escaped attribute. ?>
>
    <output data-wp-text="context.count"></output>
    <button type="button" data-wp-on--click="actions.increment">
        <?php esc_html_e( 'Increase', 'acme' ); ?>
    </button>
</div>

In view.js:

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

store( 'acme/counter', {
	actions: {
		increment() {
			const context = getContext();
			context.count += 1;
		},
	},
} );

Use wp_register_script_module() only for modules not already registered through block.json. Do not enqueue the module as a classic script.

State, context, and config

Directive rules

Common directives are:

Do not invent directive names or duplicate an attribute on the same element. When generating or modifying markup, prefer WP_HTML_Tag_Processor over regex or concatenation.

WordPress 7.1 behavior to audit

Server-side data-wp-bind now aligns more closely with the value sent to the client:

Therefore, derived state used by an attribute binding must resolve to a finite scalar or null. Do not bind an array to class, style, or another attribute and rely on PHP coercion.

Malformed directive names and missing namespaces are also handled more defensively in 7.1. Treat notices under WP_DEBUG as contract failures, not harmless noise.

Async and external callbacks

Actions invoked by the runtime receive the correct scope. If an action continues in a timer, subscription, or other external callback, preserve scope with withScope(). Follow the package's generator-based async-action pattern where the installed WordPress version requires it; do not replace it blindly with an unscoped Promise callback.

Security and performance checklist

Verification

  1. Inspect the rendered page for the data-wp-* attributes and the script module.
  2. Enable WP_DEBUG and SCRIPT_DEBUG; resolve _doing_it_wrong() and console warnings.
  3. Confirm the server-rendered value equals the hydrated value before interaction.
  4. Exercise keyboard behavior and ARIA state, not only pointer clicks.
  5. Test two block instances to expose accidental global state.
  6. Test the oldest supported WordPress version; guard the feature if it predates 6.5.

Read references/contracts-and-debugging.md for directive value semantics, lifecycle choices, and failure modes.

Related skills

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.