本文档介绍了 WordPress 块编辑器的模块化架构,强调其基于独立包(packages)构建,这些包不仅用于块编辑器本身,还可用于 WordPress 管理界面内外。模块化设计带来了代码可维护性、性能优化和第三方开发便利性等优势。
// 使用 npm 安装和使用 @wordpress/components 包
npm install @wordpress/components
import { Button } from '@wordpress/components';
function MyApp() {
return <Button>Nice looking button</Button>;
}
// 在 WordPress 插件中使用包作为脚本依赖
// myplugin.php
wp_register_script( 'myscript', 'pathtomyscript.js', array ('wp-components', "react" ) );
// 在脚本中使用
const { Button } = wp.components;
function MyApp() {
return <Button>Nice looking button</Button>;
}The WordPress block editor is based around the idea that you can combine independent blocks together to write your post or build your page. Blocks can also use and interact with each other. This makes it very modular and flexible.
But the Block Editor does not embrace modularity for its behavior and output only. The Gutenberg repository is also built from the ground up as several reusable and independent modules or packages, that, combined together, lead to the application and interface we all know. These modules are known as WordPress packages and are published and updated regularly on the npm package repository.
These packages are used to power the Block Editor, but they can be used to power any page in the WordPress Admin or outside.
Using a modular architecture has several benefits for all the actors involved:
Almost everything in the Gutenberg repository is built into a package. We can split these packages into two different types:
These are the packages that ship in WordPress itself as JavaScript scripts. These constitute the actual production code that runs on your browsers. As an example, there’s a components package serving as a reusable set of React components used to prototype and build interfaces quickly. There’s also an api-fetch package that can be used to call WordPress Rest APIs.
Third-party developers can use these production packages in two different ways:
npm install @wordpress/components
import { Button } from '@wordpress/components';
function MyApp() {
return <Button>Nice looking button</Button>;
}
wp-package-name (e.g. wp-components). Once you add the script to your own WordPress plugin scripts dependencies, the package will be available on the wp global variable.// myplugin.php
// Example of script registration depending on the "components" and "element packages.
wp_register_script( 'myscript', 'pathtomyscript.js', array ('wp-components', "react" ) );
// Using the package in your scripts
const { Button } = wp.components;
function MyApp() {
return <Button>Nice looking button</Button>;
}
Script dependencies definition can be a tedious task for developers. Mistakes and oversight can happen easily. If you want to learn how you can automate this task. Check the @wordpress/scripts and @wordpress/dependency-extraction-webpack-plugin documentation.
Some production packages provide stylesheets to function properly.
build-style folder of the package. Make sure to load this style file on your application.In the context of existing WordPress pages, if you omit to define the scripts or styles dependencies properly, your plugin might still work properly if these scripts and styles are already loaded there by WordPress or by other plugins, but it’s highly recommended to define all your dependencies exhaustively if you want to avoid potential breakage in future versions.
Some WordPress production packages define data stores to handle their state. These stores can also be used by third-party plugins and themes to retrieve data and to manipulate it. The name of these data stores is also normalized following this format core/package-name (E.g. the @wordpress/block-editor package defines and uses the core/block-editor data store).
If you’re using one of these stores to access and manipulate WordPress data in your plugins, don’t forget to add the corresponding WordPress script to your own script dependencies for your plugin to work properly. (For instance, if you’re retrieving data from the core/block-editor store, you should add the wp-block-editor package to your script dependencies like shown above).
These are packages used in development mode to help developers with daily tasks to develop, build and ship JavaScript applications, WordPress plugins and themes. They include tools for linting your codebase, building it, testing it…

It’s often surprising to new contributors to discover that the post editor is constructed as a layered abstraction of three separate packages @wordpress/edit-post, @wordpress/editor, and @wordpress/block-editor.
The above Why? section should provide some context for how individual packages aim to satisfy specific requirements. That applies to these packages as well:
@wordpress/block-editor provides components for implementing a block editor, operating on a primitive value of an array of block objects. It makes no assumptions for how this value is saved, and has no awareness (or requirement) of a WordPress site.@wordpress/editor is the enhanced version of the block editor for WordPress posts. It utilizes components from the @wordpress/block-editor package. Having an awareness of the concept of a WordPress post, it associates the loading and saving mechanism of the value representing blocks to a post and its content. It also provides various components relevant for working with a post object in the context of an editor (e.g., a post title input component). This package can support editing posts of any post type and does not assume that rendering happens in any particular WordPress screen or layout arrangement.@wordpress/edit-post is the implementation of the “New Post” (“Edit Post”) screen in the WordPress admin. It is responsible for the layout of the various components provided by @wordpress/editor and @wordpress/block-editor, with full awareness of how it is presented in the specific screen in the WordPress administrative dashboard.Structured this way, these packages can be used in a variety of combinations outside the use-case of the “New Post” screen:
@wordpress/edit-site or @wordpress/edit-widgets package can serve as similar implementations of a “Site Editor” or “Widgets Editor”, in much the same way as @wordpress/edit-post.@wordpress/editor could be used in the implementation of the “Reusable Block” block, since it is essentially a nested block editor associated with the post type wp_block.@wordpress/block-editor could be used independently from WordPress, or with a completely different save mechanism. For example, it could be used for a comments editor for posts of a site.