[wp-trac] [WordPress Trac] #39959: Clarify parameters for callbacks for `register_rest_field()`
WordPress Trac
noreply at wordpress.org
Sat May 16 21:06:08 UTC 2020
#39959: Clarify parameters for callbacks for `register_rest_field()`
--------------------------+--------------------------------
Reporter: flixos90 | Owner: TimothyBlynJacobs
Type: defect (bug) | Status: assigned
Priority: normal | Milestone: Future Release
Component: REST API | Version: 4.7
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+--------------------------------
Comment (by TimothyBlynJacobs):
I think it makes sense to deprecate
`WP_REST_Meta_Fields::register_field()` because we can't really go from an
object to an id value cleanly.
We could update the update_callback to do something like this...
{{{#!php
register_rest_field(
$this->get_rest_field_type(),
'meta',
array(
'get_callback' => function( $prepared, $field_name,
$request ) {
return $this->get_value( $prepared['id'], $request
);
},
'update_callback' => function( $data, $object ) {
if ( $object instanceof WP_Post ) {
$id = $object->ID;
} elseif ( $object instanceof WP_User ) {
$id = $object->ID;
} elseif ( $object instanceof WP_Comment ) {
$id = $object->comment_ID;
} elseif ( $object instanceof WP_Term ) {
$id = $object->term_id;
} else {
// now what?
}
return $this->update_value( $data, $id );
},
'schema' => $this->get_field_schema(),
)
);
}}}
But that only works for built in objects which wouldn't use
`register_rest_field` anyways. And doesn't do anything for non-core meta
types that might want to actually leverage the `register_rest_field`.
There is also no guarantee for the `get_callback` that the `id` property
exists in the response data for the custom endpoint.
So from my perspective, the current API methods are the most flexible way
to interact with the meta fields helper and trying to make
`register_field` work will end up being less clean than the current API.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39959#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list