_oembed_rest_pre_serve_request

函数
_oembed_rest_pre_serve_request ( $served, $result, $request, $server )
Access
Private
参数
  • (bool) $served Whether the request has already been served.
    Required:
  • (WP_HTTP_Response) $result Result to send to the client. Usually a `WP_REST_Response`.
    Required:
  • (WP_REST_Request) $request Request used to generate the response.
    Required:
  • (WP_REST_Server) $server Server instance.
    Required:
返回值
  • (true)
定义位置
相关方法
rest_ensure_requestwp_oembed_register_routerest_parse_request_argwp_create_user_requestrest_preload_api_request
引入
4.4.0
弃用
-

_oembed_rest_pre_serve_request: 这个钩子用于在REST API请求中的oEmbed响应被提供之前修改它。

钩住REST API的输出,打印XML而不是JSON。

这只针对oEmbed API端点,它支持两种格式。

function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) {
	$params = $request->get_params();

	if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) {
		return $served;
	}

	if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) {
		return $served;
	}

	// Embed links inside the request.
	$data = $server->response_to_data( $result, false );

	if ( ! class_exists( 'SimpleXMLElement' ) ) {
		status_header( 501 );
		die( get_status_header_desc( 501 ) );
	}

	$result = _oembed_create_xml( $data );

	// Bail if there's no XML.
	if ( ! $result ) {
		status_header( 501 );
		return get_status_header_desc( 501 );
	}

	if ( ! headers_sent() ) {
		$server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) );
	}

	echo $result;

	return true;
}

常见问题

FAQs
查看更多 >