Repository navigation
Description
Summary
Add support for a widgets/ folder in wp-build where developers can drop widget source code and have widgets automatically discovered, built to build/widgets/, and registered as WordPress script modules — following the same pattern as the existing routes/ support and the blocks/ proposal in #74542.
Motivation
The immediate driver for this is the admin-modernization thread outlined in the 7.0 planning post, which lists "grid layout customization to dashboard experience or other screens" among the areas to explore. That direction aligns with the dashboard proposal in #74616 (@fcoveram), where self-contained, user-arrangeable units are the core composable primitive.
Widgets (self-contained UI units with typed attributes, suitable for dashboards and similar surfaces) currently require manual wiring: authors write render code, declare metadata, register script modules, and list widgets by hand.
Applying the same file-based discovery pattern that routes/ and the proposed blocks/ use would simplify widget development (drop files in widgets/my-widget/, get automatic building and registration), remove boilerplate (no manual wp_register_script_module() calls), keep patterns consistent across wp-build entity types, and unblock server-side enumeration (the host can list available widgets without loading any JS).
That said, widgets/ support lands here as a standalone build primitive on purpose. It isn't coupled to the dashboard effort and doesn't require any specific modernization plan to ship to be useful: any plugin or screen that benefits from self-contained, file-based UI units can adopt it independently. If the eventual dashboard direction shifts, this convention can still evolve on its own merits.
Proposed Source Structure
widgets/
my-widget/
widget.json # Source of truth — static metadata (required)
widget.ts # Runtime settings — imports widget.json (optional)
render.tsx # Runtime UI component (optional)
render.scss # Component styles (optional, bundled inline)
The layout mirrors the block pattern (block.json + index.js + edit.js), adapted for widgets:
| Blocks | Widgets | Notes |
|---|---|---|
block.json |
widget.json |
static metadata, JSON source of truth |
index.js → settings |
widget.ts → settings |
runtime-only (imports, functions, i18n) |
edit.js |
render.tsx |
widgets are dynamic-only — no save.js equivalent |
editor.scss + style.scss |
render.scss |
single style entry (to revisit if needed) |
widget.json (required)
Static metadata. Readable by PHP for registration and by any server-side context that needs to enumerate widgets without executing JS.
{
"name": "my-plugin/hello-world",
"title": "Hello World",
"description": "A minimal example widget.",
"category": "demo",
"textdomain": "my-plugin"
}widget.ts (optional)
Runtime settings, analogous to a block's index.js. Imports widget.json as the source of truth and exports settings with runtime-only concerns (imported icon components, function references, example data with translated strings, runtime-computed labels).
import { __ } from '@wordpress/i18n';
import metadata from './widget.json';
const { name } = metadata;
export { metadata, name };
export const settings = {
// Runtime-only fields. JSON fields MUST NOT be redeclared here.
example: {
greeting: __( 'Hello, World!', 'my-plugin' ),
},
};Rule: anything that can be JSON goes in widget.json. widget.ts exists only for what JSON can't express.
render.tsx (optional)
Runtime UI component. Receives attributes matching the schema declared in widget.json.
interface RenderProps {
attributes: { greeting?: string };
}
export default function Render( { attributes }: RenderProps ) {
return <div>{ attributes.greeting ?? 'Hello' }</div>;
}Proposed Build Output
build/
widgets.php # Widget registration PHP (auto-generated)
widgets/
registry.php # Registry array (auto-generated)
my-widget/
widget.json # Copied verbatim from source
widget.min.js # Bundled settings entry (ESM)
widget.min.asset.php # Dependencies and version
render.min.js # Bundled render entry (ESM)
render.min.asset.php
render.min.css # Component styles (if render.scss exists)
Each widget is registered as script modules via wp_register_script_module() with handles following a namespaced pattern:
{handlePrefix}/widgets/{name}/widget
{handlePrefix}/widgets/{name}/render
Implementation Approach
wp-build changes
-
New
widget-utils.mjs— widget discovery followingroute-utils.mjs/ proposedblock-utils.mjspattern:getAllWidgets()scanswidgets/for subdirectories withwidget.json;getWidgetMetadata()parseswidget.json;getWidgetFiles()detects available files. -
New PHP templates —
widget-registry.php.templatefor the metadata array andwidget-registration.php.templatefor thewp_register_script_module()calls. -
Update
build.mjs— add the widget building phase (bundlewidget.ts+render.tsx, compilerender.scss), copywidget.jsonto build output verbatim (like blocks do), generatebuild/widgets.phpandbuild/widgets/registry.php, and add widget file watching in watch mode. -
Update
build.php.template— includewidgets.php.
PHP registration
Ship a register_widget_type_from_metadata() helper that reads widget.json from the build output and registers the associated script modules. Parallel to register_block_type_from_metadata(). Out of scope for the initial wp-build PR; tracked as a follow-up.
Features to Support
- Static metadata discovery via
widget.json - Runtime settings via
widget.ts(withwidget.jsonimport as source of truth) - Render component via
render.tsx - Component styles via
render.scss - Asset dependency tracking (
.asset.phpfiles) - Watch mode for development
- Script module registration on
inithook - Server render (
render.php) — future, if needed
Related
- wp-build: Add blocks folder support for automatic block discovery and registration #74542 — blocks in wp-build (direct precedent)
- Build: Add
widgets/folder support to @wordpress/build #77347 — widgets in wp-build (implementation PR)
Activity
simison commentedon Apr 23, 2026
@youknowriad can you help confirm if "widget" is good nomenclature-wise or if we should have something more specific like "dashboard widget" to not confuse with classic theme widgets?
[-]wp-build: Add widgets folder support for automatic widget discovery and registration[/-][+]wp-build: Add automatic widget discovery and registration[/+]