rest_is_field_included

函式
rest_is_field_included ( $field, $fields )
引數
  • (string) $field A field to test for inclusion in the response body.
    Required:
  • (array) $fields An array of string fields supported by the endpoint.
    Required:
返回值
  • (bool) Whether to include the field or not.
定義位置
相關方法
rest_is_ip_addressrest_is_integerrest_api_loadedtinymce_includerss_enclosure
引入
5.3.0
棄用
-

rest_is_field_included: 這是一個WordPress的函式,用來檢查一個欄位是否包含在REST API響應的欄位集合中: 該函式接收一個欄位名和一個欄位陣列,如果該欄位被包括,則返回真,否則返回假: 這個函式被用來過濾REST API響應中返回的欄位。

給出一個要包含在響應中的欄位陣列,其中一些可能是`巢狀.欄位’,確定所提供的欄位是否應該包含在響應體中。

如果一個父欄位被傳入,在該父欄位中存在任何巢狀欄位將導致該方法返回”true”。例如”title”,如果提供了任何一個`title`,`title.raw`或`title.rendered`,將返回true。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >