Opened 8 years ago
Closed 8 years ago
#38786 closed defect (bug) (fixed)
register_meta()'s show_in_rest not mapping to specific fields
Reported by: | garubi | Owned by: | rachelbaker |
---|---|---|---|
Milestone: | 4.7 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch has-unit-tests |
Focuses: | rest-api | Cc: |
Description
I'm following the directions found on the (future) documentation for the REST API https://github.com/WP-API/wp-api-meta-endpoints/blob/new-meta-new-life/README.md#apispecific-options.
Reading it, looks like we could register any custom field to be exposed via REST API and map it to an API-specific field name.
i.e the field could be "telephone_number" but exposed to the API as "phone".
function register_new_meta() { $args = array( //'sanitize_callback' => 'sanitize_my_meta_key', //'auth_callback' => 'authorize_my_meta_key', 'type' => 'string', 'description' => 'test register_field', 'single' => true, 'show_in_rest' => array( 'name' => 'phone' ) register_meta( 'post', 'telephone_number', $args ); }
I tested it and in the response to my GET I get the 'phone' field empty, while if I omit the 'name' => 'phone' argument, setting only show_in_rest => true like this:
add_action( 'init', 'register_new_meta'); function register_new_meta() { $args = array( 'type' => 'string', 'description' => 'Extra information', 'single' => true, 'show_in_rest' => true, ); register_meta( 'post', 'telephone_number', $args ); }
I get the 'wpcf-informazioni' field populated with its content.
So is the "mapping" to the "name" not working (maybe the document I referenced isn't updated), or am I doing something wrong... ?
Attachments (2)
Change History (20)
#3
@
8 years ago
@tharsheblows, @joehoyle just tested the patch on a test WP 4.7-beta3-39232 installation and it works as expected.
Would be great if this fix could go in the 4.7 release.
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
8 years ago
#7
@
8 years ago
While 38786.diff looks a bit hairy because of the variable renaming, it looks good to me.
#8
@
8 years ago
I added a fresh patch with the implementation to be made a lot more like the Settings endpoint, whereby the arrays are keyed by meta_key, and $args['name']
is the "visual" name in the REST API. I took the tests from @tharsheblows's patch.
This ticket was mentioned in Slack in #core-restapi by joehoyle. View the logs.
8 years ago
#12
@
8 years ago
@rmccue this seems like a good one to get into either 4.7.1 or 4.7.2, do you agree?
#13
@
8 years ago
- Keywords fixed-major added
- Milestone changed from 4.7 to 4.7.2
@jnylen0 Too late for 4.7.1 I think, but I agree, given the new release cycle.
#14
@
8 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
👍🏻 Reopening so that someone can port this to the 4.7 branch.
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
8 years ago
This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.
8 years ago
#17
@
8 years ago
- Owner changed from rmccue to rachelbaker
- Status changed from reopened to assigned
Reassigning this to you @rachelbaker to review for backporting.
#18
@
8 years ago
- Keywords fixed-major removed
- Milestone changed from 4.7.3 to 4.7
- Resolution set to fixed
- Status changed from assigned to closed
@jnylen0 @rmccue
Looks like r39328 is already in the 4.7 branch (see: https://core.trac.wordpress.org/browser/branches/4.7/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php?annotate=blame#L68) because it came along when 4.7 was branched with r39356.
Closing this with the correct milestone of 4.7.
First pass at a fix. nb: I didn't want the stored meta key names to leak out in error messages if someone used a custom name. (Ie if there's an error message, it should use
$name
from'show_in_rest' => array( 'name' => $name )
)