admin管理员组

文章数量:1384098

I know how to register custom endpoints for Wordpress REST API and actually use it to output podcasts from my website like this:

register_rest_route( 'myradio/v1', '/podcasts/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'myradio_podcasts_api_endpoint',
    'args' => array(
        'id' => array(
            'description' => esc_html__( 'Unique identifier for the object.', 'myradio' ),
            'type'        => 'integer',
            'prop_format' => 'int64',
            'validate_callback' => function($param, $request, $key) {
                return is_numeric( $param );
            },
        ),
    ),
));

Now I want to register an additional route to allow users to subscribe to my podcasts. For that I offer a RSS feed with the same data, right now as a custom link using a wordpress template, setting the header as Content-Type: application/rss+xml.

To integrate this functionality directly in the REST API I want to register a RSS feeds endpoint adding /rss to the route with something like that:

register_rest_route( 'myradio/v1', '/podcasts/(?P<id>\d+/rss)', .array(...) );

Where and how do I define the response content type as application/rss+xml?

When I tried to echo the RSS feed directly at my callback function, the browser complains about an not well-formed document because the header is application/json.

本文标签: Wordpress Rest API custom endpoint for RSS feed