Opened 2 months ago
Last modified 2 months ago
#64423 new feature request
Introduce register_content_type() API for declarative content modeling
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | trunk |
| Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests |
| Focuses: | Cc: |
Description
This ticket proposes a new register_content_type() function that provides a higher-level, declarative API for registering custom post types with their associated meta fields in a single call.
Background
Currently, registering a custom post type with structured data requires separate calls to register_post_type() and multiple calls to register_post_meta(). This pattern is verbose, error-prone, and requires developers to manually coordinate REST API schema generation across these separate registrations.
See the full RFC: https://docs.google.com/document/d/1Z8ei9vOsj_TvyApq1EKet9BhzA4zdm8uFPd9cXJWpJs/edit
Proposal
Introduce register_content_type() which:
- Registers a post type via
register_post_type() - Registers meta fields via
register_post_meta()for each declared field - Provides automatic REST API schema generation
- Supports field type validation (string, integer, number, boolean, array, object)
- Stores UI hints for potential editor/admin integrations
Example Usage
register_content_type( 'book', array(
'labels' => array( 'name' => 'Books' ),
'public' => true,
'show_in_rest' => true,
'fields' => array(
'isbn' => array(
'type' => 'string',
'required' => true,
'label' => 'ISBN',
),
'published_year' => array(
'type' => 'integer',
'label' => 'Published Year',
),
),
) );
New Functions
register_content_type()/unregister_content_type()content_type_exists()/get_content_type_object()/get_content_types()get_content_type_fields()/get_content_type_field()get_content_type_ui()/get_content_type_rest_schema()validate_content_type_values()
Try It
Live demo in WordPress Playground - includes a "Books" content type with sample data.
Implementation
Pull request: https://github.com/WordPress/wordpress-develop/pull/10617
Files added:
src/wp-includes/class-wp-content-type.php- Core classsrc/wp-includes/content-type.php- API functionstests/phpunit/tests/post/wpContentType.php- Unit tests