Overview
The@wordpress/edit-widgets package provides the block-based Widgets editor screen for WordPress. It bridges the gap between traditional widgets and modern blocks, allowing you to use both in widget areas while maintaining full backward compatibility.
This package is designed for WordPress core and may not be fully documented for external use. However, you can use it in your own projects with the understanding that APIs may evolve.
Three-Layer Architecture
The Widgets editor follows the same three-layer architecture as other WordPress editors:Layer Responsibilities
@wordpress/block-editor- Generic block editing canvas. Provides the foundation for block manipulation.@wordpress/editor- WordPress entity management. Handles entity persistence and data operations.@wordpress/edit-widgets- Widgets screen implementation. Manages widget areas (sidebars), widget-to-block translation, and legacy widget compatibility.
Installation
Install the package via npm:@wordpress/babel-preset-default.
How the Widgets Editor Works
The new Widgets editor in WordPress admin is another block editor, just like the post editor or site editor. However, it has a unique challenge: it must support both Gutenberg blocks and traditional widgets.The Translation Mechanism
To support both widgets and blocks, the editor employs a bidirectional translation mechanism:Widget Block (Widgets as Blocks)
A widget block acts as a block UI wrapper for widget data. This allows traditional widgets to be edited within the block editor:- Edit Mode - Shows the standard widget form (the same form you’d see in the old widgets screen)
- Preview Mode - Displays a server-side render of the widget output
Block Widget (Blocks as Widgets)
A block widget acts as a storage mechanism for blocks added to widget areas. This is a special HTML widget that stores block markup:- All blocks added to widget areas are stored as these special HTML widgets
- The block data is stored as it’s rendered by the block’s save function
- This ensures complete compatibility with the old widgets screen
This dual translation mechanism ensures 100% compatibility with the old widgets screen. If the block-based editor breaks some widget functionality, you can easily revert to the old screen and continue editing legacy widgets.
Widget Areas (Sidebars)
The widgets editor manages widget areas, also known as sidebars:Adding Widgets to Areas
REST API Endpoints
The widgets editor uses several dedicated REST API endpoints:Widgets Endpoint (/wp/v2/widgets)
Used to:
- Load and save widgets
- Retrieve server-side render of widget edit forms
- Manage widget instances
Sidebars Endpoint (/wp/v2/sidebars)
Used to:
- List available widget areas
- Assign widgets to widget areas
- Remove widgets from widget areas
Widget Types Endpoint (/wp/v2/widget-types)
Used to:
- List available widget types (e.g., text widget, calendar widget)
- Get widget type information and capabilities
- Determine which widgets support block transformations
Widget Block Variations
To make the experience seamless for users, the widgets editor automatically registers widget block variations:- Search for widgets by their exact name
- Insert widgets using the block inserter
- See familiar widget names instead of generic “Widget” block
Hiding Redundant Widgets
Widgets that have block equivalents can be hidden to avoid confusion:All core widgets that have block equivalents are automatically hidden in the widgets editor. Users should use the block versions for better compatibility and features.
Legacy Widget Compatibility
The widgets editor provides full backward compatibility:Widget Form Integration
Traditional widget forms are embedded within the block editor:- User inserts a widget block and selects a widget type
- The editor loads the widget’s edit form via AJAX
- The form is rendered within the block editor interface
- Changes are auto-saved as the user edits
- Widget data is stored in the traditional widget format
Fallback to Classic Widgets Screen
If you encounter compatibility issues:Block Editor Settings
You can customize the widgets editor behavior:Working with Widget Data
Widget data follows a specific structure:Batch Processing
This package contains an experimental batch processing feature:@wordpress/batch-processing package.
Customizing Widget Areas
You can programmatically manage widget areas:Common Use Cases
Migrating from Classic Widgets to Blocks
To encourage users to migrate from widgets to blocks:- Identify widgets with block equivalents
- Hide the legacy widgets using filters
- Provide documentation on equivalent blocks
- Consider creating custom blocks for specialized widgets
Supporting Custom Legacy Widgets
If you have custom widgets that don’t have block equivalents:Related Packages
@wordpress/block-editor- Generic block editing functionality@wordpress/editor- WordPress entity-aware editor features@wordpress/core-data- WordPress entity management and REST API integration@wordpress/widgets- Widget management utilities@wordpress/interface- Shared interface components