WordPress开发者和主题制作者的搜索引擎



rest_ensure_response ›

Since4.4.0
已弃用n/a
rest_ensure_response ( $response )
参数:
  • (WP_Error|WP_HTTP_Response|mixed) $response Response to check.
    Required: Yes
返回:
  • (WP_REST_Response|mixed) If response generated an error, WP_Error, if response is already an instance, WP_HTTP_Response, otherwise returns a new WP_REST_Response instance.
定义在:
文档:

Ensures a REST response is a response object (for consistency).

This implements WP_HTTP_Response, allowing usage of set_status/header/etc without needing to double-check the object. Will also allow WP_Error to indicate error responses, so users should immediately check for this value.



源码

function rest_ensure_response( $response ) {
	if ( is_wp_error( $response ) ) {
		return $response;
	}

	if ( $response instanceof WP_HTTP_Response ) {
		return $response;
	}

	return new WP_REST_Response( $response );
}