交互性 API 是 WordPress 6.5 引入的标准方法,用于为区块前端添加交互功能,支持从简单计数器到复杂购物车等丰富用户体验。该 API 允许区块间共享数据、操作和回调,简化通信并减少错误。
npm install @wordpress/interactivity --saveimport { 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>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.
@wordpress/interactivity which is bundled in WordPress Core from v6.5
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:
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+).
interactivity to your projectInstall 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';
interactivity support to block.jsonTo 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.
viewScriptModuleThe 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 yourpackage.jsonscripts 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"
}
}
wp-interactive directive to a DOM elementTo “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.
Here you have some more resources to learn/read more about the Interactivity API: