Skip to content

Register fields for entities in the server #74865

Open

Description

@oandregal

What

In Gutenberg, we've experimented with the ability to register fields #67175 for a given entity by means of a client-side function, wp.editor.registerEntityField, which is avaliable when the Gutenberg plugin is active.

We want to go a step further and enable 3rd parties to extend the existing DataViews/DataForm based screens declaratively, from the server. This includes the Site Editor screens for Pages, Templates, and Patterns.

Why

To make it as easy as possible to extend and work with entities (pages, template, patterns).

How

  • Bootstrap field collections #76332
  • Expand the system so that fields that require JavaScript can also be declared.
    • The wordpress/fields packages bundles all fields together but we actually want to have separate bundles per collection (entity) and load them on demand.
  • Migrate all fields to use the new system
    • wordpress/fields
    • wordpress/media-fields
    • fields declared locally for Site Editor's Patterns
    • fields declared locally for Site Editor's Templates
  • Screens: site editor v1
    • Templates
    • Template parts
    • Patterns
    • Posts/Pages
      • Editor: usePostFields (link)
  • Screens: site editor v2

Activity

  1. added
    [Feature] DataViewsWork surrounding upgrading and evolving views in the site editor and beyond
    on Jan 22, 2026
  2. andrewserong commented on Feb 2, 2026

    @andrewserong
    Contributor

    Just jotting down a quick comment that for the case of media / attachments, a server-side approach for registering fields for entities should also be able to support (or map) to the filter for attachment_fields_to_edit. I.e. for each field added via that filter, attempt to register an attachment entity field for DataViews/DataForm where possible.

    I've written that up in #75123 as it's not an immediate requirement for this issue, but will be for the media editor / registering fields for the attachment entity. So just something for us to keep in mind.

  3. oandregal commented on Feb 17, 2026

    @oandregal
    MemberAuthor

    Removed from the 7.0 project as this is not making it to WordPress 7.0 — we've focused on other features instead.

  4. oandregal commented on Mar 9, 2026

    @oandregal
    MemberAuthor

    I've started to work on this: added some initial tasks in the issue description, and prepared #76332 to bootstrap the server-side registration API.

  5. jorgefilipecosta commented on Oct 20, 2026

    @jorgefilipecosta
    Member

    Here is a small plugin to test the fields API from PHP and a script module. It's tested on WordPress trunk with the Gutenberg build.

    It adds a Subtitle field to pages, stored in the demo_subtitle post meta:

    • wp_register_entity_fields() registers the field on wp_entity_fields_api_init.
    • The script module provides getValue and setValue, which read and write the value through meta.
    • The get_entity_view_config_posttype_page filter merges the field into the default Pages view and into the form.

    To test, add both files to wp-content/plugins/demo-page-field/ and activate the plugin. The field shows in Site Editor > Pages (list and table layouts, after resetting a saved view), in Quick Edit, and in the page editor's document sidebar, and editing it in Quick Edit or the editor saves the meta.

    demo-page-field.php:

    <?php
    /**
     * Plugin Name: Demo Page Field
     * Description: Adds a "Subtitle" entity field to pages, stored in post meta (Entity Fields API, WordPress 7.2).
     * Version:     1.0.0
     * Requires at least: 7.2
     */
    
    add_action(
    	'init',
    	static function () {
    		register_post_meta(
    			'page',
    			'demo_subtitle',
    			array(
    				'type'              => 'string',
    				'single'            => true,
    				'default'           => '',
    				'show_in_rest'      => true,
    				'sanitize_callback' => 'sanitize_text_field',
    			)
    		);
    
    		wp_register_script_module( 'demo-page-field', plugins_url( 'fields.js', __FILE__ ), array(), '1.0.0' );
    	}
    );
    
    add_action(
    	'wp_entity_fields_api_init',
    	static function () {
    		wp_register_entity_fields(
    			'demo-page-field',
    			'postType',
    			'page',
    			array(
    				array(
    					'id'            => 'demo_subtitle',
    					'type'          => 'text',
    					'label'         => __( 'Subtitle', 'demo-page-field' ),
    					'description'   => __( 'A short line shown under the page title.', 'demo-page-field' ),
    					'enableSorting' => false,
    					'filterBy'      => false,
    				),
    			),
    			'demo-page-field'
    		);
    	}
    );
    
    // Show the field in the Pages list and in the page editor's inspector.
    add_filter(
    	'get_entity_view_config_posttype_page',
    	static function ( $data ) {
    		return $data->merge(
    			array(
    				'default_view' => array( 'fields' => array( 'demo_subtitle' ) ),
    				'form'         => array( 'fields' => array( 'demo_subtitle' ) ),
    			),
    			1
    		);
    	}
    );

    fields.js:

    // The JavaScript parts of the fields registered in demo-page-field.php, keyed by field id.
    // The value lives in post meta, so it is read from and written to `meta`.
    export default {
    	demo_subtitle: {
    		getValue: ( { item } ) => item.meta?.demo_subtitle ?? '',
    		setValue: ( { value } ) => ( { meta: { demo_subtitle: value } } ),
    	},
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Feature] DataViewsWork surrounding upgrading and evolving views in the site editor and beyond[Type] EnhancementA suggestion for improvement.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions