Skip to content

wp-build: Add automatic widget discovery and registration #77576

Closed

Description

@retrofox

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

  1. New widget-utils.mjs — widget discovery following route-utils.mjs / proposed block-utils.mjs pattern: getAllWidgets() scans widgets/ for subdirectories with widget.json; getWidgetMetadata() parses widget.json; getWidgetFiles() detects available files.

  2. New PHP templates — widget-registry.php.template for the metadata array and widget-registration.php.template for the wp_register_script_module() calls.

  3. Update build.mjs — add the widget building phase (bundle widget.ts + render.tsx, compile render.scss), copy widget.json to build output verbatim (like blocks do), generate build/widgets.php and build/widgets/registry.php, and add widget file watching in watch mode.

  4. Update build.php.template — include widgets.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 (with widget.json import as source of truth)
  • Render component via render.tsx
  • Component styles via render.scss
  • Asset dependency tracking (.asset.php files)
  • Watch mode for development
  • Script module registration on init hook
  • Server render (render.php) — future, if needed

Related

Activity

  1. self-assigned this
    on Apr 22, 2026
  2. simison commented on Apr 23, 2026

    @simison
    Member

    @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?

  3. changed the title [-]wp-build: Add widgets folder support for automatic widget discovery and registration[/-] [+]wp-build: Add automatic widget discovery and registration[/+] on Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Package] wp-build

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions