块编辑器开发文档

交互性 API 参考

💡 云策文档标注

概述

交互性 API 是 WordPress 6.5 引入的标准方法,用于为区块前端添加交互功能,支持从简单计数器到复杂购物车等丰富用户体验。该 API 允许区块间共享数据、操作和回调,简化通信并减少错误。

关键要点

  • 交互性 API 自 WordPress 6.5 起集成于核心,低版本需 Gutenberg 17.5+。
  • 开发环境要求包括代码编辑器、Node.js 工具和本地 WordPress 站点。
  • 通过 npm 安装 @wordpress/interactivity 包,并在 block.json 中设置 "interactivity": true 以启用支持。
  • 使用 viewScriptModule 加载 JavaScript 代码,需在 wp-scripts 中添加 --experimental-modules 标志。
  • 在 DOM 元素中添加 wp-interactive 指令以激活交互性 API 区域。

代码示例

npm install @wordpress/interactivity --save
import { store } from '@wordpress/interactivity';
"supports": {
    "interactivity": true
}
{
    "viewScriptModule": "file:./view.js"
}
{
    "scripts": {
        "build": "wp-scripts build --experimental-modules",
        "start": "wp-scripts start --experimental-modules"
    }
}
<div data-wp-interactive="myPlugin">
    <!-- Interactivity API zone -->
</div>

注意事项

  • 使用 @wordpress/create-block-interactive-template 脚手架时,package.json 已自动包含 --experimental-modules 标志。
  • 交互性 API 文档包括快速入门、教程、核心概念和示例资源,建议按顺序阅读以深入理解。

📄 原文内容

The Interactivity API, introduced in WordPress 6.5, provides a standard way for developers to add interactions to the front end of their blocks. The API is also used in many Core WordPress blocks, including Search, Query, Navigation, and File.

This standard makes it easier for developers to create rich, interactive user experiences, from simple counters or pop-ups to more complex features like instant page navigation, instant search, shopping carts, or checkouts.

Blocks can share data, actions, and callbacks between them. This makes communication between blocks simpler and less error-prone. For example, clicking on an “add to cart” block can seamlessly update a separate “cart” block.

For more information about the genesis of the Interactivity API, check out the original proposal. You can also review the merge announcement, the status update post, and the official Trac ticket.

The Interactivity API is supported by the package @wordpress/interactivity which is bundled in WordPress Core from v6.5

Navigating the Interactivity API docs

Use the following links to locate the topic you’re interested in. If you have never worked with the Interactivity API before, consider reading through the following resources in the order listed.

To get a deeper understanding of what the Interactivity API is or find answers to questions you may have about this standard, check the following resources:

  • About the Interactivity API: To learn more about the API Goals and the reasoning behind the use of a standard to add interactivity to blocks.
  • Frequently Asked Questions: To find responses to some frequently asked questions about the technology behind and alternatives.

Requirements of the Interactivity API

Interactivity API is included in Core in WordPress 6.5. For versions below, you’ll need Gutenberg 17.5 or higher installed and activated in your WordPress installation.

It’s also important to highlight that the block creation workflow doesn’t change, and all the prerequisites remain the same. These include:

You can start creating interactions once you set up a block development environment and run WordPress 6.5+ (or Gutenberg 17.5+).

Code requirements

Add interactivity to your project

Install the Interactivity API to your project with the following command:

npm install @wordpress/interactivity --save

Import the store into your view.js. Refer to the store documentation for more information.

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

Add interactivity support to block.json

To indicate that the block supports the Interactivity API features, add "interactivity": true to the supports attribute of the block’s block.json file.

In block.json:

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

Refer to the interactivity support property docs to get a more detailed description of this property.

Load Interactivity API JavaScript code with viewScriptModule

The Interactivity API provides the @wordpress/interactivity Script Module. JavaScript using the Interactivity API should be implemented as Script Modules so they can depend on @wordpress/interactivity. Script Modules have been available since WordPress 6.5. Blocks can use viewScriptModule block metadata to enqueue their Script Modules easily:

In block.json:

{
    "viewScriptModule": "file:./view.js"
}

The use of viewScriptModule also requires the --experimental-modules flag for both the build and start scripts of wp-scripts to ensure a proper build of the Script Modules.

Note: If you scaffolded your block using the @wordpress/create-block-interactive-template, this flag is already included in your package.json scripts and no manual configuration is needed.

If you need to add it manually, update your package.json:

{
    "scripts": {
        "build": "wp-scripts build --experimental-modules",
        "start": "wp-scripts start --experimental-modules"
    }
}

Add wp-interactive directive to a DOM element

To “activate” the Interactivity API in a DOM element (and its children), add the wp-interactive directive to the DOM element in the block’s render.php or save.js files.

<div data-wp-interactive="myPlugin">
    <!-- Interactivity API zone -->
</div>

Refer to the wp-interactive documentation for a more detailed description of this directive.

Docs & Examples

Here you have some more resources to learn/read more about the Interactivity API:

There’s a Tracking Issue opened to ease the coordination of the work related to the Interactivity API Docs: Documentation for the Interactivity API – Tracking Issue #53296