0

I know that there is a JSON API Plugin and I think that is what I want with the major exception being that it only outputs JSON and there is no configuration that I can see for outputting XML. Is there an equivalent for an XML webservice or can I output it myself through some built-in WP functionality?

I'd like to output XML because I am looking to feed the data into a third party search engine tool that can integrate with XML web services.

1 Answer 1

3

WordPress ships with an XML-RPC interface that's enabled by default. It gives you the ability to both retrieve and edit/create content on the site via XML.

There's quite a bit of detail about the API available on the Codex.


Updated

There aren't (to my knowledge) any existing systems in place that do XML over GET for WordPress. Webpages tend to be some form of (X)HTML already and, if following the XHTML spec closely, your theme alone might produce something parse-able.

However, that's not the only option in place.

If you take a look at some of the newer AMP work being done with WordPress, you'll see how developers are leveraging custom URL rewrite endpoints to serve different content for the same resource. For example:

You could use the same pattern to code an /xml rewrite endpoint that presents your post in whatever arbitrary markup as you need.

To get you started ...

Enable an endpoint

function xml_init() {
    add_rewrite_endpoint( 'xml', EP_PERMALINK );
}
add_action( 'init', 'xml_init' );

Intercept requests so you can prepare XML

function xml_maybe_change_markup() {
    if ( ! is_singular() || is_feed() ) return;

    // Only intercept the right requests
    if ( false === get_query_var( 'xml', false ) ) return;

    add_action( 'template_redirect', 'xml_render' );
}
add_action( 'wp', 'xml_maybe_change_markup' );

Actually render XML

function xml_render() {
    $post_id = get_queried_object_id();

    // Get the post, build your XML document, print it to the page

    exit;
}

As I don't know what exact output you need, I leave building the XML document as an exercise for you :-)

4
  • Yes, I came across this while searching around. But can it be accessed straight away with an HTTP request to a URL? It seems like XML-RPC is a library of functions that require a script to call and process the responses. Sep 30, 2016 at 19:05
  • XML-RPC is accessed by URL, but must be done via POST requests (not the typical GETs triggered by a browser). If you're trying to grab XML content for another application, you can trigger a POST via tools like curl. If you're trying to specifically get WordPress to deliver XML to the browser, you'll need some other mechanism.
    – EAMann
    Sep 30, 2016 at 20:51
  • Thanks for the clarification. Yes, I'm looking to make a GET request, so I'll have to look elsewhere. Sep 30, 2016 at 21:09
  • Made a few changes to explain how you'd go about doing that on your own. Good luck!
    – EAMann
    Sep 30, 2016 at 21:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.