1

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.

8
  • 2
    If your podcast is a CPT or a taxonomy term, you could just visit its archive and put /feed on the end of the URL? You shouldn't need any code to do that, WP does it out of the box
    – Tom J Nowell
    Apr 24, 2020 at 15:00
  • Great info, that works out for most cases. But for some there is no base taxonomy or CPT. Any chance to create a custom rss endpoint? I think of another situation where I need a geojson endpoint to show them on a map. So finally the question is about how to create a endpoint with a different Response Content Type as described in OpenAPI with responses.<code>.content.<media-type>.
    – geraldo
    Apr 24, 2020 at 16:53
  • That's a different Q to what you asked, just know that making a REST API endpoint respond with an RSS feed is not a good idea, it should respond with a REST response, aka JSON
    – Tom J Nowell
    Apr 24, 2020 at 20:04
  • REST response normally doesn't mean exclusively JSON, but ok, I read now that Wordpress understands REST exclusivly as sending and receiving data as JSON. So the first comment to my question should be the accepted answer right?
    – geraldo
    Apr 25, 2020 at 14:15
  • You can make it respond with something other than JSON, but it's a weird thing to do, adding /feed to the end of a REST API endpoint in WP won't turn it into an RSS though. Keep in mind these aren't answers, they're comments. You should details exactly what podcasts are stored as in your Q, otherwise adding the feed URL modifier is the normal answer ( though this doesn't apply in your case, I'm assuming you're using a custom table to store these )
    – Tom J Nowell
    Apr 25, 2020 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.