[wp-trac] [WordPress Trac] #35658: Provide additional data for registered meta through register_meta()
WordPress Trac
noreply at wordpress.org
Sun Jan 31 14:17:48 UTC 2016
#35658: Provide additional data for registered meta through register_meta()
------------------------------------------+------------------------------
Reporter: jeremyfelt | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Options, Meta APIs | Version:
Severity: normal | Resolution:
Keywords: needs-patch needs-unit-tests | Focuses:
------------------------------------------+------------------------------
Comment (by tharsheblows):
A minor correction: for backwards compatibility the function would include
a fourth parameter, $auth_callback which is a callback for capability
checks.
So we ended the discussion @jeremyfelt mentioned with something like this
- I've included all the suggestions above to see what it looks like:
{{{
// $object_args is the object type (post, user etc) and optional string or
array of subtype(s)
$object_args = array(
'post' => array( 'post', 'cpt-one', 'cpt-two' ), // a post object with
post type post, cpt-one or cpt-two
'term', // a term object
);
// $meta_key is the meta_key
$meta_key = 'my_meta_key';
// $args is the array with all the new args. If is_callable, it is taken
to be the sanitization callback.
$args = array(
'data_type' => 'string', // to use in REST api schema
'description' => 'This is the meta key description.', // to use in
REST api schema,
'single' => false, // to use in REST api schema and response as
@joehoyle suggested
'show_in_rest' => true, // should it be included in REST api schema
and response at all
'sanitize_callback' => 'my_sanitization_callback', // the custom
sanitization callback
);
// $auth_callback is the authorization callback and needed for back
compatibility
$auth_callback = 'my_auth_callback';
register_meta( $object_args, $meta_key, $args, $auth_callback );
}}}
There was also a discussion around how unwieldy the $wp_meta_keys global
would be so we thought it would make it more manageable to have a function
get_registered_meta which narrows it down a bit, something like:
{{{
// eg instead of
$wp_meta_keys[$object_type][$object_subtype][$key]['description'];
$object_args = array( 'post' => 'cpt-one' );
$key = 'my_meta_key';
$registered_meta = get_registered_meta( $object_args, $key );
$description = $registered_meta['description']; // this should be 'This is
the meta key description.'
}}}
@sc0ttkclark This came about after a discussion about how to build the
REST api schema for meta and determine whether or not a meta_key should be
included at all in contexts which don't require authorization. We need to
be able to get the meta key metadata (eg 'data_type', 'description') but
there might be a better way or something which works better with the plans
for the Fields api.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/35658#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list