_oembed_rest_pre_serve_request

Hooks into the REST API output to print XML instead of JSON.

Description

_oembed_rest_pre_serve_request( (bool) $served, (WP_HTTP_ResponseInterface) $result, (WP_REST_Request) $request, (WP_REST_Server) $server ); 

This is only done for the oEmbed API endpoint, which supports both formats.

Parameters (4)

0. $served (bool)
Whether the request has already been served.
1. $result (WP_HTTP_ResponseInterface)
Result to send to the client. Usually a WP_REST_Response.
2. $request (WP_REST_Request)
Request used to generate the response.
3. $server (WP_REST_Server)
Server instance.

Usage

  1. if ( !function_exists( '_oembed_rest_pre_serve_request' ) ) { 
  2. require_once ABSPATH . WPINC . '/embed.php'; 
  3.  
  4. // Whether the request has already been served. 
  5. $served = true; 
  6.  
  7. // Result to send to the client. Usually a WP_REST_Response. 
  8. $result = null; 
  9.  
  10. // Request used to generate the response. 
  11. $request = null; 
  12.  
  13. // Server instance. 
  14. $server = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = _oembed_rest_pre_serve_request($served, $result, $request, $server); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/embed.php  
  1. function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { 
  2. $params = $request->get_params(); 
  3.  
  4. if ( '/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method() ) { 
  5. return $served; 
  6.  
  7. if ( ! isset( $params['format'] ) || 'xml' !== $params['format'] ) { 
  8. return $served; 
  9.  
  10. // Embed links inside the request. 
  11. $data = $server->response_to_data( $result, false ); 
  12.  
  13. if ( ! class_exists( 'SimpleXMLElement' ) ) { 
  14. status_header( 501 ); 
  15. die( get_status_header_desc( 501 ) ); 
  16.  
  17. $result = _oembed_create_xml( $data ); 
  18.  
  19. // Bail if there's no XML. 
  20. if ( ! $result ) { 
  21. status_header( 501 ); 
  22. return get_status_header_desc( 501 ); 
  23.  
  24. if ( ! headers_sent() ) { 
  25. $server->send_header( 'Content-Type', 'text/xml; charset=' . get_option( 'blog_charset' ) ); 
  26.  
  27. echo $result; 
  28.  
  29. return true;