On this page
rest_is_field_included( string $field, array $fields ): bool
Given an array of fields to include in a response, some of which may be 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.
Parameters
$fieldstring Required-
A field to test for inclusion in the response body.
$fieldsarray Required-
An array of string fields supported by the endpoint.
Return
bool Whether to include the field or not.
Source
File: wp-includes/rest-api.php. View all references
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 ( strpos( $accepted_field, "$field." ) === 0 ) {
return true;
}
// Conversely, if "parent" is accepted, all "parent.child" fields
// should also be accepted.
if ( strpos( $field, "$accepted_field." ) === 0 ) {
return true;
}
}
return false;
}
Related
Used By
| Used By | Description |
|---|---|
| WP_REST_Block_Patterns_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php | Prepare a raw block pattern before it gets output in a REST API response. |
| WP_REST_Block_Pattern_Categories_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php | Prepare a raw block pattern category before it gets output in a REST API response. |
| WP_REST_Menu_Items_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares a single post output for response. |
| WP_REST_Global_Styles_Controller::get_theme_item() wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php | Returns the given theme global styles config. |
| WP_REST_Global_Styles_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php | Prepare a global styles config output for response. |
| WP_REST_Menus_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php | Prepares a single term output for response. |
| WP_REST_Menu_Locations_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php | Prepares a menu location object for serialization. |
| WP_REST_Widgets_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php | Prepares the widget for the REST response. |
| WP_REST_Sidebars_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php | Prepares a single sidebar output for response. |
| WP_REST_Templates_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php | Prepare a single template output for response |
| WP_REST_Widget_Types_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php | Prepares a widget type object for serialization. |
| WP_REST_Application_Passwords_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php | Prepares the application password for the REST response. |
| WP_REST_Block_Directory_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php | Parse block metadata for a block, and prepare it for an API response. |
| WP_REST_Plugins_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php | Prepares the plugin for the REST response. |
| WP_REST_Block_Types_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php | Prepares a block type object for serialization. |
| WP_REST_Search_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php | Prepares a single search result for response. |
| WP_REST_Themes_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php | Prepares a single theme output for response. |
| WP_REST_Users_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Prepares a single user output for response. |
| WP_REST_Terms_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php | Prepares a single term output for response. |
| WP_REST_Posts_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post output for response. |
| WP_REST_Taxonomies_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php | Prepares a taxonomy object for serialization. |
| WP_REST_Post_Types_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php | Prepares a post type object for serialization. |
| WP_REST_Controller::add_additional_fields_to_object() wp-includes/rest-api/endpoints/class-wp-rest-controller.php | Adds the values from additional fields to a data object. |
| WP_REST_Comments_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Prepares a single comment output for response. |
Changelog
| Version | Description |
|---|---|
| 5.3.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_is_field_included