块编辑器开发文档

💡 云策文档标注

概述

本文档介绍了如何扩展和自定义 WordPress 新编辑器(基于块),涵盖块创建、编辑器 UI 扩展、主题支持等核心功能,面向开发者提供灵活的开发指南。

关键要点

  • 块创建:使用 Block API 创建静态块、Dynamic Blocks(服务器端渲染)和可保存数据到 Post Meta 的块。
  • 块扩展:通过过滤器修改或移除现有块,特别是 Query Loop 块有更多自定义方式。
  • 编辑器 UI 扩展:使用 registerPlugin API 集中定义插件 UI 元素,并利用编辑器过滤器。
  • 元框迁移:鼓励将 PHP meta boxes 迁移到块或侧边栏插件。
  • 主题支持:块提供默认样式,主题可添加、覆盖样式或依赖默认值,高级功能需主题选择支持。
  • 自动完成:可扩展和覆盖块内的自动完成功能。
  • 块解析与序列化:编辑器中的帖子在存储和显示间经历解析和序列化步骤,支持自定义解析器。

📄 原文内容

The new editor is highly flexible, like most of WordPress. You can build custom blocks, modify the editor’s appearance, add special plugins, and much more.

Creating blocks

The editor is about blocks, and the main extensibility API is the Block API. It allows you to create your own static blocks, Dynamic Blocks ( rendered on the server ) and also blocks capable of saving data to Post Meta for more structured content.

If you want to learn more about block creation, see the Create a Block tutorial for the best place to start.

Extending blocks

It is also possible to modify the behavior of existing blocks or even remove them completely using filters.

Learn more in the Block Filters section.

Specifically for Query Loop block, besides the available filters, there are more ways to extend it and create bespoke versions of it. Learn more in the Extending the Query Loop block section.

Extending the Editor UI

Extending the editor UI can be accomplished with the registerPlugin API, allowing you to define all your plugin’s UI elements in one place.

Refer to the Plugins and Edit Post section for more information.

You can also filter certain aspects of the editor; this is documented on the Editor Filters page.

Meta boxes

Porting PHP meta boxes to blocks or sidebar plugins is highly encouraged, learn how in the meta box and sidebar plugin guides.

Theme support

By default, blocks provide their styles to enable basic support for blocks in themes without any change. Themes can add/override these styles, or rely on defaults.

There are some advanced block features which require opt-in support in the theme. See theme support and how to filter global styles.

Autocomplete

Autocompleters within blocks may be extended and overridden. Learn more about the autocomplete filters.

Block parsing and serialization

Posts in the editor move through a couple of different stages between being stored in post_content and appearing in the editor. Since the blocks themselves are data structures that live in memory it takes a parsing and serialization step to transform out from and into the stored format in the database.

Customizing the parser is an advanced topic that you can learn more about in the Extending the Parser section.