rest_is_field_included( string $field, array $fields ): bool
- Since
- 5.3.0
- Source
wp-includes/rest-api.php:977
Checks whether a single field name is present in an array of allowed REST response fields, matching dotted parent.child names in either direction. It backs the field-filtering logic in WP_REST_Controller::add_additional_fields_to_object() and the prepare_item_for_response() methods of most core REST controllers. Because the initial comparison is a strict in_array() check, field names must match exactly in case and spelling to be recognized as a direct hit before the parent/child fallback runs.
nested.fields, determine whether the provided field should be included in the response body.Description
If a parent field is passed in, the presence of any nested field within that parent will cause the method to return true. For example "title" will return true if any of title, title.raw or title.rendered is provided.
Compatibility
- WordPress
- since 5.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$fieldstring- A field to test for inclusion in the response body.
$fieldsarray- An array of string fields supported by the endpoint.
Return value
bool- Whether to include the field or not.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Test whether a field survives a REST _fields filter, including nested dotted fields
Run the function directly against a sample requested-fields array to see how parent and child field names resolve.
$requested_fields = array( 'id', 'title.rendered', 'meta.price' );
$checks = array(
'title' => rest_is_field_included( 'title', $requested_fields ),
'title.raw' => rest_is_field_included( 'title.raw', $requested_fields ),
'meta' => rest_is_field_included( 'meta', $requested_fields ),
'meta.price' => rest_is_field_included( 'meta.price', $requested_fields ),
'author' => rest_is_field_included( 'author', $requested_fields ),
);
echo '<pre>';
foreach ( $checks as $field => $included ) {
printf( "%-12s => %s\n", esc_html( $field ), $included ? 'true' : 'false' );
}
echo '</pre>';title.raw is included even though only title.rendered was requested, because the plain 'title' rule matches any of its children, but the demo asks about title.raw specifically to show that a sibling child field is not accepted on its own; check the printed output rather than assuming.
Skip an expensive REST field calculation unless it was actually requested
Register a computed field on posts that only does the work when the client's _fields list actually asks for it, either by name or as part of a parent field.
add_action( 'rest_api_init', function() {
register_rest_field(
'post',
'reading_time',
array(
'get_callback' => function( $post_arr, $field_name, $request ) {
$requested = $request->get_fields();
if ( ! empty( $requested ) && ! rest_is_field_included( $field_name, $requested ) ) {
return null;
}
$post = get_post( $post_arr['id'] );
$words = str_word_count( wp_strip_all_tags( $post->post_content ) );
return array(
'words' => $words,
'minutes' => (int) ceil( $words / 200 ),
);
},
'schema' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
),
)
);
} );After activating, load /wp-json/wp/v2/posts/1?_fields=title,reading_time.minutes in a browser tab to see reading_time returned, and reload without the _fields parameter or with a different one to see it disappear.
Common problems and fixes · 3
- Why isn't my requested field being included even though it's spelled right in _fields?
- Why does requesting a parent field pull in fields I never asked for?
- Why does passing an empty $fields array make every field come back excluded?
Why isn't my requested field being included even though it's spelled right in _fields?
Why does requesting a parent field pull in fields I never asked for?
Why does passing an empty $fields array make every field come back excluded?
Alternatives and related functions
WP_REST_Controller::get_fields_for_response- When you need the whole resolved list of fields for a request rather than testing one field name at a time.
WP_REST_Controller::add_additional_fields_to_object- When you're preparing a REST response object and want the standard additional-fields loop, which already calls this function for you.
WP_REST_Request::get_fields- When you need the parsed array of requested fields from the current request's _fields parameter to pass in as the $fields argument.
Performance profile
How much work a call to rest_is_field_included() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 6–15
- Plugin surface
- None
- Called by
- 33
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 20.
Nothing here hands control to plugin code.
33 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_is_field_included() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–15 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 20 | 6–15 | 5 | |
| 8.5 | 20 | 6–15 | 5 | |
| 8.4 | 20 | 6–15 | 5 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 29 | 9–24 | 5 | |
| 8.2 | 29 | 9–24 | 5 | |
| 8.1 | 29 | 9–24 | 5 | |
| 7.4 | 29 | 9–24 | 5 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
Used by · 33
- WP_REST_Application_Passwords_Controller::prepare_item_for_response()Prepares the application password for the REST response.
- WP_REST_Block_Directory_Controller::prepare_item_for_response()Parse block metadata for a block, and prepare it for an API response.
- WP_REST_Block_Pattern_Categories_Controller::prepare_item_for_response()Prepare a raw block pattern category before it gets output in a REST API response.
- WP_REST_Block_Patterns_Controller::prepare_item_for_response()Prepare a raw block pattern before it gets output in a REST API response.
- WP_REST_Block_Types_Controller::prepare_item_for_response()Prepares a block type object for serialization.
- WP_REST_Comments_Controller::prepare_item_for_response()Prepares a single comment output for response.
- WP_REST_Controller::add_additional_fields_to_object()Adds the values from additional fields to a data object.
- WP_REST_Font_Collections_Controller::prepare_item_for_response()Prepare a single collection output for response.
- WP_REST_Font_Faces_Controller::prepare_item_for_response()Prepares a single font face output for response.
- WP_REST_Font_Families_Controller::prepare_item_for_response()Prepares a single font family output for response.
- WP_REST_Global_Styles_Controller::get_theme_item()Returns the given theme global styles config.
- WP_REST_Global_Styles_Controller::prepare_item_for_response()Prepare a global styles config output for response.
Show all 33
- WP_REST_Global_Styles_Revisions_Controller::prepare_item_for_response()Prepares the revision for the REST response.
- WP_REST_Menu_Items_Controller::prepare_item_for_response()Prepares a single nav menu item output for response.
- WP_REST_Menu_Locations_Controller::prepare_item_for_response()Prepares a menu location object for serialization.
- WP_REST_Menus_Controller::prepare_item_for_response()Prepares a single term output for response.
- WP_REST_Navigation_Fallback_Controller::prepare_item_for_response()Matches the post data to the schema we want.
- WP_REST_Plugins_Controller::prepare_item_for_response()Prepares the plugin for the REST response.
- WP_REST_Post_Types_Controller::prepare_item_for_response()Prepares a post type object for serialization.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Revisions_Controller::prepare_item_for_response()Prepares the revision for the REST response.
- WP_REST_Search_Controller::prepare_item_for_response()Prepares a single search result for response.
- WP_REST_Server::get_index()Retrieves the site index.
- WP_REST_Sidebars_Controller::prepare_item_for_response()Prepares a single sidebar output for response.
- WP_REST_Taxonomies_Controller::prepare_item_for_response()Prepares a taxonomy object for serialization.
- WP_REST_Template_Autosaves_Controller::prepare_item_for_response()Prepares the item for the REST response.
- WP_REST_Template_Revisions_Controller::prepare_item_for_response()Prepares the item for the REST response.
- WP_REST_Templates_Controller::prepare_item_for_response()Prepare a single template output for response
- WP_REST_Terms_Controller::prepare_item_for_response()Prepares a single term output for response.
- WP_REST_Themes_Controller::prepare_item_for_response()Prepares a single theme output for response.
- WP_REST_Users_Controller::prepare_item_for_response()Prepares a single user output for response.
- WP_REST_Widget_Types_Controller::prepare_item_for_response()Prepares a widget type object for serialization.
- WP_REST_Widgets_Controller::prepare_item_for_response()Prepares the widget for the REST response.
Source code
function rest_is_field_included( $field, $fields ) { if ( in_array( $field, $fields, true ) ) { return true; } foreach ( $fields as $accepted_field ) { /* * Check to see if $field is the parent of any item in $fields. * A field "parent" should be accepted if "parent.child" is accepted. */ if ( str_starts_with( $accepted_field, "$field." ) ) { return true; } /* * Conversely, if "parent" is accepted, all "parent.child" fields * should also be accepted. */ if ( str_starts_with( $field, "$accepted_field." ) ) { return true; } } return false;}Changelog
Introduced in 5.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.