Opened 7 months ago
Last modified 2 months ago
#64423 new feature request
Introduce register_content_type() API for declarative content modeling
| Reported by: | whyisjake | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Posts, Post Types | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
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
Change History (3)
This ticket was mentioned in Slack in #core by whyisjake. View the logs.
7 months ago
#3
@
2 months ago
- Version trunk
Not introduced in 7.0, so removing trunk.
Possibly related: https://github.com/WordPress/gutenberg/issues/77600.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Removing
trunkversion as this is not going to be shipped with WP 7.0 but in the next releases.