wp-interactivity-api by wordpress/agent-skills
npx skills add https://github.com/wordpress/agent-skills --skill wp-interactivity-api当用户提及以下内容时使用此技能:
@wordpress/interactivitydata-wp-interactive、data-wp-on--*、data-wp-bind--*、data-wp-contextviewScriptModule / 基于模块的视图脚本wp-project-triage)广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
搜索:
data-wp-interactive@wordpress/interactivityviewScriptModule判断:
block.json 视图脚本模块提供交互性的区块?如果你正在创建一个新的交互式区块(不仅仅是调试),建议使用官方脚手架模板:
@wordpress/create-block-interactive-template(通过 @wordpress/create-block)定位存储定义并确认:
data-wp-on--* 使用的回调/事件处理器在输出到页面之前在服务器上预渲染 HTML,以确保:
对于使用 block.json 的组件,添加 supports.interactivity:
{
"supports": {
"interactivity": true
}
}
对于没有 block.json 的主题/插件,使用 wp_interactivity_process_directives() 来处理指令。
使用 wp_interactivity_state() 定义初始全局状态:
wp_interactivity_state( 'myPlugin', array(
'items' => array( 'Apple', 'Banana', 'Cherry' ),
'hasItems' => true,
));
对于本地上下文,使用 wp_interactivity_data_wp_context():
<?php
$context = array( 'isOpen' => false );
?>
<div <?php echo wp_interactivity_data_wp_context( $context ); ?>>
...
</div>
当派生状态影响初始 HTML 渲染时,在 PHP 中复制该逻辑:
wp_interactivity_state( 'myPlugin', array(
'items' => array( 'Apple', 'Banana' ),
'hasItems' => function() {
$state = wp_interactivity_state();
return count( $state['items'] ) > 0;
}
));
这确保了像 data-wp-bind--hidden="!state.hasItems" 这样的指令在首次加载时能正确渲染。
有关详细示例和模式,请参阅 references/server-side-rendering.md。
处理标记指令时:
WordPress 6.9 变更:
data-wp-ignore 已弃用,并将在未来版本中移除。它破坏了上下文继承并导致客户端导航问题。请避免使用它。--- 分隔符存在多个相同类型的指令(例如,data-wp-on--click---plugin-a="..." 和 data-wp-on--click---plugin-b="...")。AsyncAction<ReturnType> 和 TypeYield<T> 有助于异步操作的类型定义。如需快速指令提醒,请参阅 references/directives-quickref.md。
验证仓库是否支持所需的模块构建路径:
@wordpress/scripts,请遵循其约定。如果交互时“没有任何反应”:
viewScriptModule 已入队/加载data-wp-interactive 属性请参阅 references/debugging.md。
wp-project-triage 在你更改后显示 signals.usesInteractivityApi: true(如果适用)data-wp-interactivewp_interactivity_state()block.json 中未设置 supports.interactivity(针对区块)wp_interactivity_process_directives()(针对主题/插件)state.hasItems),导致 hidden 属性缺失getServerState() 和 getServerContext() 现在在页面切换之间会重置——确保你的代码不假定过时的值会持续存在attachTo 用于动态渲染覆盖层(模态框、弹出窗口)@wordpress/scripts 还是自定义打包工具(webpack/vite)?”references/server-side-rendering.mdreferences/directives-quickref.mdreferences/debugging.md每周安装数
373
仓库
GitHub 星标数
985
首次出现
2026年2月1日
安全审计
安装于
codex259
opencode255
cursor254
gemini-cli250
github-copilot247
kimi-cli237
Use this skill when the user mentions:
@wordpress/interactivity,data-wp-interactive, data-wp-on--*, data-wp-bind--*, data-wp-context,viewScriptModule / module-based view scripts,wp-project-triage).Search for:
data-wp-interactive@wordpress/interactivityviewScriptModuleDecide:
block.json view script module?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)Locate store definitions and confirm:
data-wp-on--*.Pre-render HTML on the server before outputting to ensure:
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.
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>
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.
When touching markup directives:
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.--- separator (e.g., data-wp-on--click---plugin-a="..." and data-wp-on--click---plugin-b="...").AsyncAction<ReturnType> and TypeYield<T> help with async action typing.For quick directive reminders, see references/directives-quickref.md.
Verify the repo supports the required module build path:
@wordpress/scripts, prefer its conventions.If “nothing happens” on interaction:
viewScriptModule is enqueued/loaded,data-wp-interactive,See references/debugging.md.
wp-project-triage indicates signals.usesInteractivityApi: true after your change (if applicable).data-wp-interactive.wp_interactivity_state() with closures.supports.interactivity not set in block.json (for blocks).wp_interactivity_process_directives() not called (for themes/plugins).state.hasItems missing on server, causing hidden attribute to be absent.@wordpress/scripts or a custom bundler (webpack/vite)?"references/server-side-rendering.mdreferences/directives-quickref.mdreferences/debugging.mdWeekly Installs
373
Repository
GitHub Stars
985
First Seen
Feb 1, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex259
opencode255
cursor254
gemini-cli250
github-copilot247
kimi-cli237
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装
getServerState() and getServerContext() now reset between page transitions—ensure your code doesn't assume stale values persist.attachTo for rendering overlays (modals, pop-ups) dynamically.