Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren



rest_filter_response_fields ›

Seit4.8.0
Veraltetn/v
rest_filter_response_fields ( $response, $server, $request )
Parameter: (3)
  • (WP_REST_Response) $response Current response being served.
    Erforderlich: Ja
  • (WP_REST_Server) $server ResponseHandler instance (usually WP_REST_Server).
    Erforderlich: Ja
  • (WP_REST_Request) $request The request that was used to make current response.
    Erforderlich: Ja
Gibt zurück:
  • (WP_REST_Response) Response to be served, trimmed down to contain a subset of fields.
Definiert in:
Codex:

Filter the API response to include only a white-listed set of response object fields.



Quellcode

function rest_filter_response_fields( $response, $server, $request ) {
	if ( ! isset( $request['_fields'] ) || $response->is_error() ) {
		return $response;
	}

	$data = $response->get_data();

	$fields = is_array( $request['_fields'] ) ? $request['_fields'] : preg_split( '/[\s,]+/', $request['_fields'] );

	if ( 0 === count( $fields ) ) {
		return $response;
	}

	// Trim off outside whitespace from the comma delimited list.
	$fields = array_map( 'trim', $fields );

	$fields_as_keyed = array_combine( $fields, array_fill( 0, count( $fields ), true ) );

	if ( wp_is_numeric_array( $data ) ) {
		$new_data = array();
		foreach ( $data as $item ) {
			$new_data[] = array_intersect_key( $item, $fields_as_keyed );
		}
	} else {
		$new_data = array_intersect_key( $data, $fields_as_keyed );
	}

	$response->set_data( $new_data );

	return $response;
}