rest_is_field_included() WordPress Function

The rest_is_field_included() function is used to check whether a given field is included in the current request.

rest_is_field_included( string $field, array $fields ) #

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.


Top ↑

Parameters

$field

(string)(Required)A field to test for inclusion in the response body.

$fields

(array)(Required)An array of string fields supported by the endpoint.


Top ↑

Return

(bool) Whether to include the field or not.


Top ↑

Source

File: wp-includes/rest-api.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More