#45220 closed defect (bug) (fixed)
register_rest_field() doesn't work when schema is empty
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 5.0 | Priority: | normal |
| Severity: | normal | Version: | 5.0 |
| Component: | REST API | Keywords: | has-patch has-unit-tests fixed-5.0 |
| Focuses: | rest-api | Cc: |
Description
Just descovered that on the latest Nightly build (5.0-beta1-43832) the new field registerd with register_rest_field is not returned at all.
Works fine on 4.9.8
Attachments (2)
Change History (22)
#2
@
8 years ago
Hi, thank you for your response.
It is very simple:
<?php add_action( 'rest_api_init', 'yasr_rest_comment_meta' ); function yasr_rest_comment_meta() { register_rest_field( 'post', 'yasr_overall_rating', array( 'get_callback' => 'yasr_rest_get_overall_rating', 'update_callback' => null, 'schema' => null, ) ); } function yasr_rest_get_overall_rating () { $overall_rating = yasr_get_overall_rating(); return $overall_rating; }
#4
@
8 years ago
nope, the js part is just this
fetch('http://192.168.33.10/plugin_development/wp-json/wp/v2/posts/3569').then(response => {
return response.json();
}).then(data => {
// Work with JSON data here
console.log(data);
}).catch(err => {
// Do something for an error here
});
#5
@
8 years ago
This is because the schema is set to null so get_fields_for_resposne() doesn't pick it up since it isn't registered in the schema.
#6
@
8 years ago
seems like is working with this code
<?php function yasr_rest_comment_meta() { register_rest_field( 'post', 'yasr_overall_rating', array( 'get_callback' => 'yasr_rest_get_overall_rating', 'update_callback' => null, 'schema' => $overall_rating_schema, ) ); $overall_rating_schema = array( 'description' => 'Overall Rating for the post', 'type' => 'string', 'context' => array( 'view' ) ); }
I think the "schema" part needs more documentation.
#7
@
8 years ago
With version 5.0-beta2-43846 doesn't work with both 'schema' declared or set to null
#8
@
8 years ago
Until then the scheme argument was optional a and by default null. If i use the _fields , have to specify all fields, including the default fields (id, title, content...).
I would like to show just my custom fields. What is the best option? What should I do with the legacy projects without the scheme argument?
#9
@
8 years ago
With version, 5.0-beta4-43896 too doesn't work with both 'schema' declared or set to null.
This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.
8 years ago
#13
@
8 years ago
- Keywords needs-patch needs-unit-tests added; reporter-feedback removed
I was able to reproduce this issue by registering a custom field like follows:
add_action( 'rest_api_init', function() {
register_rest_field( 'post', 'test_field', array(
'get_callback' => function() {
return 'test_value';
},
'schema' => null,
) );
});
In WP 4.9.8, the test_field attribute is present for GET http://wptest.test/wp-json/wp/v2/posts/1. In WP 5.0-beta5-43907, the test_field attribute is missing.
Where did it go?!
#14
@
8 years ago
- Keywords has-patch has-unit-tests commit added; needs-patch needs-unit-tests removed
- Summary changed from register_rest_field doesn't work to register_rest_field() doesn't work when schema is empty
The culprit was r43736.
In 45220.1.diff, ?_fields= filtering is only applied when ?_fields= is present in the request. I think this is the proper way to go about this, but it's the Friday of a very long week so I'm going to let it sit until early next.
#15
@
8 years ago
In 45220.2.diff, any fields registered with register_rest_field() that have a null schema are included in WP_REST_Controller::get_fields_for_response(). I think this is a more appropriate fix because:
- It ensures
get_fields_for_response()has the expected return values. - It includes
my_custom_intin the response when the request is?_fields=title,my_custom_int.
#16
@
8 years ago
- Owner set to danielbachhuber
- Resolution set to fixed
- Status changed from new to closed
In 43908:
Hey there and welcome to WordPress Trac!
Could you please show us the code that you're using? Without that it's hard to reproduce.