3 * WP_oEmbed_Controller class, used to provide an oEmbed endpoint.
11 * oEmbed API endpoint controller.
13 * Registers the API route and delivers the response data.
14 * The output format (XML or JSON) is handled by the REST API.
18 final class WP_oEmbed_Controller {
20 * Register the oEmbed REST API route.
24 public function register_routes() {
26 * Filter the maxwidth oEmbed parameter.
30 * @param int $maxwidth Maximum allowed width. Default 600.
32 $maxwidth = apply_filters( 'oembed_default_width', 600 );
34 register_rest_route( 'oembed/1.0', '/embed', array(
36 'methods' => WP_REST_Server::READABLE,
37 'callback' => array( $this, 'get_item' ),
41 'sanitize_callback' => 'esc_url_raw',
45 'sanitize_callback' => 'wp_oembed_ensure_format',
48 'default' => $maxwidth,
49 'sanitize_callback' => 'absint',
57 * Callback for the API endpoint.
59 * Returns the JSON object for the post.
63 * @param WP_REST_Request $request Full data about the request.
64 * @return WP_Error|array oEmbed response data or WP_Error on failure.
66 public function get_item( $request ) {
67 $post_id = url_to_postid( $request['url'] );
70 * Filter the determined post ID.
74 * @param int $post_id The post ID.
75 * @param string $url The requested URL.
77 $post_id = apply_filters( 'oembed_request_post_id', $post_id, $request['url'] );
79 $data = get_oembed_response_data( $post_id, $request['maxwidth'] );
82 return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );