wp-block-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWP Block Development
WordPress区块开发
When to use
适用场景
Use this skill for block work such as:
- creating a new block, or updating an existing one
- changing (scripts/styles/supports/attributes/render/viewScriptModule)
block.json - fixing “block invalid / not saving / attributes not persisting”
- adding dynamic rendering (/
render.php)render_callback - block deprecations and migrations (versions)
deprecated - build tooling for blocks (,
@wordpress/scripts,@wordpress/create-block)wp-env
本技能适用于以下区块开发工作:
- 创建新区块或更新现有区块
- 修改(脚本/样式/supports/属性/渲染/viewScriptModule)
block.json - 修复“区块无效/无法保存/属性不持久”问题
- 添加动态渲染(/
render.php)render_callback - 区块弃用与迁移(版本)
deprecated - 区块构建工具(、
@wordpress/scripts、@wordpress/create-block)wp-env
Inputs required
所需输入
- Repo root and target (plugin vs theme vs full site).
- The block name/namespace and where it lives (path to if known).
block.json - Target WordPress version range (especially if using modules / ).
viewScriptModule
- 仓库根目录及目标位置(插件、主题或完整站点)。
- 区块名称/命名空间及其所在位置(若已知,提供的路径)。
block.json - 目标WordPress版本范围(尤其在使用模块/时)。
viewScriptModule
Procedure
操作流程
0) Triage and locate blocks
0) 分类排查与区块定位
- Run triage:
node skills/wp-project-triage/scripts/detect_wp_project.mjs
- List blocks (deterministic scan):
node skills/wp-block-development/scripts/list_blocks.mjs
- Identify the block root (directory containing ) you’re changing.
block.json
If this repo is a full site ( present), be explicit about which plugin/theme contains the block.
wp-content/- 运行分类排查脚本:
node skills/wp-project-triage/scripts/detect_wp_project.mjs
- 列出所有区块(确定性扫描):
node skills/wp-block-development/scripts/list_blocks.mjs
- 确定你要修改的区块根目录(包含的目录)。
block.json
如果仓库是完整站点(存在),请明确说明该区块属于哪个插件/主题。
wp-content/1) Create a new block (if needed)
1) 创建新区块(若需要)
If you are creating a new block, prefer scaffolding rather than hand-rolling structure:
- Use to scaffold a modern block/plugin setup.
@wordpress/create-block - If you need Interactivity API from day 1, use the interactive template.
Read:
references/creating-new-blocks.md
After scaffolding:
- Re-run the block list script and confirm the new block root.
- Continue with the remaining steps (model choice, metadata, registration, serialization).
若要创建新区块,优先使用脚手架而非手动构建结构:
- 使用搭建现代化的区块/插件架构。
@wordpress/create-block - 如果从一开始就需要Interactivity API,请使用交互式模板。
参考文档:
references/creating-new-blocks.md
脚手架搭建完成后:
- 重新运行区块列表脚本,确认新区块的根目录。
- 继续执行后续步骤(模型选择、元数据、注册、序列化)。
2) Ensure apiVersion 3 (WordPress 6.9+)
2) 确保使用apiVersion 3(WordPress 6.9+)
WordPress 6.9 enforces in the block.json schema. Blocks with apiVersion 2 or lower trigger console warnings when is enabled.
apiVersion: 3SCRIPT_DEBUGWhy this matters:
- WordPress 7.0 will run the post editor in an iframe regardless of block apiVersion.
- apiVersion 3 ensures your block works correctly inside the iframed editor (style isolation, viewport units, media queries).
Migration: Changing from version 2 to 3 is usually as simple as updating the field in . However:
apiVersionblock.json- Test in a local environment with the iframe editor enabled.
- Ensure any style handles are included in (styles missing from the iframe won't apply).
block.json - Third-party scripts attached to a specific may have scoping issues.
window
Read:
- (apiVersion and schema details)
references/block-json.md
WordPress 6.9强制要求 schema中使用。当启用时,使用apiVersion 2或更低版本的区块会触发控制台警告。
block.jsonapiVersion: 3SCRIPT_DEBUG重要性说明:
- WordPress 7.0将在iframe中运行文章编辑器,无论区块使用哪个apiVersion。
- apiVersion 3确保你的区块在iframe编辑器中正常工作(样式隔离、视窗单位、媒体查询)。
迁移步骤: 从版本2升级到3通常只需更新中的字段。但需注意:
block.jsonapiVersion- 在启用iframe编辑器的本地环境中进行测试。
- 确保所有样式句柄都已包含在中(iframe中缺失的样式将无法生效)。
block.json - 附加到特定的第三方脚本可能会出现作用域问题。
window
参考文档:
- (apiVersion及schema详情)
references/block-json.md
3) Pick the right block model
3) 选择合适的区块模型
- Static block (markup saved into post content): implement ; keep attributes serialization stable.
save() - Dynamic block (server-rendered): use in
render(orblock.jsonin PHP) and keeprender_callbackminimal orsave().null - Interactive frontend behavior:
- Prefer for modern module-based view scripts where supported.
viewScriptModule - If you're working primarily on directives or stores, also use
data-wp-*.wp-interactivity-api
- Prefer
- 静态区块(标记保存到文章内容中):实现函数;保持属性序列化稳定。
save() - 动态区块(服务器端渲染):在中使用
block.json(或在PHP中使用render),并将render_callback设为极简或save()。null - 交互式前端行为:
- 优先在支持的环境中使用作为基于模块的现代视图脚本。
viewScriptModule - 若主要处理指令或存储,同时使用
data-wp-*。wp-interactivity-api
- 优先在支持的环境中使用
4) Update block.json
safely
block.json4) 安全更新block.json
block.jsonMake changes in the block’s , then confirm registration matches metadata.
block.jsonFor field-by-field guidance, read:
references/block-json.md
Common pitfalls:
- changing breaks compatibility (treat it as stable API)
name - changing saved markup without adding causes “Invalid block”
deprecated - adding attributes without defining source/serialization correctly causes “attribute not saving”
在区块的中进行修改,然后确认注册信息与元数据匹配。
block.json如需逐字段指导,请参考:
references/block-json.md
常见陷阱:
- 修改会破坏兼容性(将其视为稳定API)
name - 修改已保存的标记但未添加会导致“区块无效”
deprecated - 添加属性但未正确定义source/序列化规则会导致“属性无法保存”
5) Register the block (server-side preferred)
5) 注册区块(优先使用服务器端)
Prefer PHP registration using metadata, especially when:
- you need dynamic rendering
- you need translations ()
wp_set_script_translations - you need conditional asset loading
Read and apply:
references/registration.md
优先使用PHP基于元数据注册区块,尤其是在以下场景:
- 需要动态渲染
- 需要翻译支持()
wp_set_script_translations - 需要条件性加载资源
参考并应用:
references/registration.md
6) Implement edit/save/render patterns
6) 实现编辑/保存/渲染模式
Follow wrapper attribute best practices:
- Editor:
useBlockProps() - Static save:
useBlockProps.save() - Dynamic render (PHP):
get_block_wrapper_attributes()
Read:
references/supports-and-wrappers.md- (if dynamic)
references/dynamic-rendering.md
遵循包装器属性最佳实践:
- 编辑器:
useBlockProps() - 静态保存:
useBlockProps.save() - 动态渲染(PHP):
get_block_wrapper_attributes()
参考文档:
references/supports-and-wrappers.md- (若使用动态渲染)
references/dynamic-rendering.md
7) Inner blocks (block composition)
7) 内部区块(区块组合)
If your block is a “container” that nests other blocks, treat Inner Blocks as a first-class feature:
- Use to integrate inner blocks with wrapper props.
useInnerBlocksProps() - Keep migrations in mind if you change inner markup.
Read:
references/inner-blocks.md
如果你的区块是用于嵌套其他区块的“容器”,请将内部区块视为核心功能:
- 使用将内部区块与包装器属性集成。
useInnerBlocksProps() - 若修改内部标记,请考虑迁移方案。
参考文档:
references/inner-blocks.md
8) Attributes and serialization
8) 属性与序列化
Before changing attributes:
- confirm where the attribute value lives (comment delimiter vs HTML vs context)
- avoid the deprecated attribute source
meta
Read:
references/attributes-and-serialization.md
修改属性前:
- 确认属性值的存储位置(注释分隔符、HTML或上下文)
- 避免使用已弃用的属性源
meta
参考文档:
references/attributes-and-serialization.md
9) Migrations and deprecations (avoid "Invalid block")
9) 迁移与弃用(避免“区块无效”)
If you change saved markup or attributes:
- Add a entry (newest → oldest).
deprecated - Provide for old versions and an optional
saveto normalize attributes.migrate
Read:
references/deprecations.md
若修改已保存的标记或属性:
- 添加条目(按从新到旧的顺序)。
deprecated - 为旧版本提供函数,可选择性添加
save函数以标准化属性。migrate
参考文档:
references/deprecations.md
10) Tooling and verification commands
10) 工具与验证命令
Prefer whatever the repo already uses:
- (common) → run existing npm scripts
@wordpress/scripts - (common) → use for local WP + E2E
wp-env
Read:
references/tooling-and-testing.md
优先使用仓库已有的工具:
- (常用)→ 运行现有npm脚本
@wordpress/scripts - (常用)→ 用于本地WordPress环境及端到端测试
wp-env
参考文档:
references/tooling-and-testing.md
Verification
验证步骤
- Block appears in inserter and inserts successfully.
- Saving + reloading does not create “Invalid block”.
- Frontend output matches expectations (static: saved markup; dynamic: server output).
- Assets load where expected (editor vs frontend).
- Run the repo’s lint/build/tests that triage recommends.
- 区块出现在插入器中并能成功插入。
- 保存并重新加载后不会出现“区块无效”提示。
- 前端输出符合预期(静态区块:已保存的标记;动态区块:服务器输出)。
- 资源在预期位置加载(编辑器端 vs 前端)。
- 运行分类排查推荐的仓库代码检查/构建/测试脚本。
Failure modes / debugging
故障模式与调试
If something fails, start here:
- (common failures + fastest checks)
references/debugging.md - (attributes not saving)
references/attributes-and-serialization.md - (invalid block after change)
references/deprecations.md
若出现问题,从以下文档开始排查:
- (常见故障+快速检查)
references/debugging.md - (属性无法保存)
references/attributes-and-serialization.md - (修改后出现区块无效)
references/deprecations.md
Escalation
升级处理
If you’re uncertain about upstream behavior/version support, consult canonical docs first:
- WordPress Developer Resources (Block Editor Handbook, Theme Handbook, Plugin Handbook)
- Gutenberg repo docs for bleeding-edge behaviors
若对上游行为/版本支持不确定,请先查阅官方文档:
- WordPress开发者资源(区块编辑器手册、主题手册、插件手册)
- Gutenberg仓库文档(前沿功能)