wp-interactivity-api

作者: wordpress

在构建或调试WordPress Interactivity API功能时使用(data-wp-*指令、@wordpress/interactivity存储/状态/动作、块viewScriptModule…)

npx skills add https://github.com/wordpress/agent-skills --skill wp-interactivity-api

WP Interactivity API

When to use

Use this skill when the user mentions:

  • Interactivity API, @wordpress/interactivity,
  • data-wp-interactive, data-wp-on--*, data-wp-bind--*, data-wp-context,
  • block viewScriptModule / module-based view scripts,
  • hydration issues or “directives don’t fire”.

Inputs required

  • Repo root + triage output (wp-project-triage).
  • Which block/theme/plugin surfaces are affected (frontend, editor, both).
  • Any constraints: WP version, whether modules are supported in the build.

Procedure

1) Detect existing usage + integration style

Search for:

  • data-wp-interactive
  • @wordpress/interactivity
  • viewScriptModule

Decide:

  • Is this a block providing interactivity via block.json view script module?
  • Is this theme-level interactivity?
  • Is this plugin-side “enhance existing markup” usage?

If you’re creating a new interactive block (not just debugging), prefer the official scaffold template:

  • @wordpress/create-block-interactive-template (via @wordpress/create-block)

2) Identify the store(s)

Locate store definitions and confirm:

  • state shape,
  • actions (mutations),
  • callbacks/event handlers used by data-wp-on--*.

3) Server-side rendering (best practice)

Pre-render HTML on the server before outputting to ensure:

  • Correct initial state in the HTML before JavaScript loads (no layout shift).
  • SEO benefits and faster perceived load time.
  • Seamless hydration when the client-side JavaScript takes over.

Enable server directive processing

For components using block.json, add supports.interactivity:

{
  "supports": {
    "interactivity": true
  }
}

For themes/plugins without block.json, use wp_interactivity_process_directives() to process directives.

Initialize state/context in PHP

Use wp_interactivity_state() to define initial global state:

wp_interactivity_state( 'myPlugin', array(
  'items'    => array( 'Apple', 'Banana', 'Cherry' ),
  'hasItems' => true,
));

For local context, use wp_interactivity_data_wp_context():

<?php
$context = array( 'isOpen' => false );
?>
<div <?php echo wp_interactivity_data_wp_context( $context ); ?>>
  ...
</div>

Define derived state in PHP

When derived state affects initial HTML rendering, replicate the logic in PHP:

wp_interactivity_state( 'myPlugin', array(
  'items'    => array( 'Apple', 'Banana' ),
  'hasItems' => function() {
    $state = wp_interactivity_state();
    return count( $state['items'] ) > 0;
  }
));

This ensures directives like data-wp-bind--hidden="!state.hasItems" render correctly on first load.

For detailed examples and patterns, see references/server-side-rendering.md.

4) Implement or change directives safely

When touching markup directives:

  • keep directive usage minimal and scoped,
  • prefer stable data attributes that map clearly to store state,
  • ensure server-rendered markup + client hydration align.

WordPress 6.9 changes:

  • data-wp-ignore is deprecated and will be removed in future versions. It broke context inheritance and caused issues with client-side navigation. Avoid using it.
  • Unique directive IDs: Multiple directives of the same type can now exist on one element using the --- separator (e.g., data-wp-on--click---plugin-a="..." and data-wp-on--click---plugin-b="...").
  • New TypeScript types: AsyncAction<ReturnType> and TypeYield<T> help with async action typing.

For quick directive reminders, see references/directives-quickref.md.

5) Build/tooling alignment

Verify the repo supports the required module build path:

  • if it uses @wordpress/scripts, prefer its conventions.
  • if it uses custom bundling, confirm module output is supported.

6) Debug common failure modes

If “nothing happens” on interaction:

  • confirm the viewScriptModule is enqueued/loaded,
  • confirm the DOM element has data-wp-interactive,
  • confirm the store namespace matches the directive’s value,
  • confirm there are no JS errors before hydration.

See references/debugging.md.

Verification

  • wp-project-triage indicates signals.usesInteractivityApi: true after your change (if applicable).
  • Manual smoke test: directive triggers and state updates as expected.
  • If tests exist: add/extend Playwright E2E around the interaction path.

Failure modes / debugging

  • Directives present but inert:
    • view script not loading, wrong module entrypoint, or missing data-wp-interactive.
  • Hydration mismatch / flicker:
    • server markup differs from client expectations; simplify or align initial state.
    • derived state not defined in PHP: use wp_interactivity_state() with closures.
  • Initial content missing or wrong:
    • supports.interactivity not set in block.json (for blocks).
    • wp_interactivity_process_directives() not called (for themes/plugins).
    • state/context not initialized in PHP before render.
  • Layout shift on load:
    • derived state like state.hasItems missing on server, causing hidden attribute to be absent.
  • Performance regressions:
    • overly broad interactive roots; scope interactivity to smaller subtrees.
  • Client-side navigation issues (WordPress 6.9):
    • getServerState() and getServerContext() now reset between page transitions—ensure your code doesn't assume stale values persist.
    • Router regions now support attachTo for rendering overlays (modals, pop-ups) dynamically.

Escalation

  • If repo build constraints are unclear, ask: "Is this using @wordpress/scripts or a custom bundler (webpack/vite)?"
  • Consult:
    • references/server-side-rendering.md
    • references/directives-quickref.md
    • references/debugging.md

来自 wordpress 的更多技能

blueprint
wordpress
在创建、编辑或审查WordPress Playground蓝图JSON文件时使用。当提及蓝图、Playground配置或请求时触发…
official
wordpress-router
wordpress
对WordPress代码库进行分类,并根据插件、主题、区块及核心检出结果路由至正确的工作流程。运行自动化项目分类以识别仓库类型(插件、主题、区块主题、Gutenberg区块、WP核心)及可用工具。输出分类结果及基于用户意图和项目类型的决策树路由至领域特定技能。需要仓库根目录访问权限及bash/Node文件系统操作;部分工作流程需WP-CLI。目标环境为WordPress 6.9+及PHP 7.2.24+;...
official
wp-abilities-api
wordpress
WordPress Abilities API 注册、REST 暴露及客户端消费,适用于 WordPress 6.9+。使用 wp_register_ability() 和 wp_register_ability_category() 在 PHP 中注册能力和类别,包含稳定 ID、标签和元数据。通过设置 meta.show_in_rest: true,将能力暴露给客户端,使用 /wp-json/wp-abilities/v1/ REST 端点。在 JavaScript 中使用 @wordpress/abilities 包消费能力,实现客户端访问和权限检查。需要 WordPress 6.9+...
official
wp-abilities-audit
wordpress
审计WordPress插件的REST接口面,并生成一份标准化的审计文档,提出Abilities API注册建议。生成一份包含YAML……的markdown文档。
official
wp-abilities-verify
wordpress
验证WordPress插件的Abilities API注册:枚举能力,检查回调行为是否与每个注解的声明相符(对抗性…
official
wp-block-development
wordpress
WordPress区块开发(针对Gutenberg):元数据、注册、渲染及构建工作流。涵盖区块创建、block.json配置、静态与动态渲染,以及使用register_block_type_from_metadata()进行服务端PHP注册。强制使用apiVersion: 3以确保与WordPress 6.9+兼容,包括iframe编辑器支持和样式隔离。处理属性序列化、弃用/迁移以避免"无效区块"错误,以及内部区块组合。包括...
official
wp-block-themes
wordpress
WordPress区块主题开发:theme.json、模板、样式块及站点编辑器故障排查。涵盖theme.json编辑(预设、设置、逐块样式)、模板与模板部件、样式块及WordPress 6.9+版本中的样式变体。包含用于检测主题根目录和区块主题结构的分类脚本,以及创建新主题或转换经典主题的引导流程。提供样式层级问题、用户自定义覆盖及站点编辑器调试的工作流程。
official
wp-patterns
wordpress
生成技术上正确、设计上独具特色的WordPress块模式。用于创建块模式、起始页面模式、模板模式、模板……
official