1

I'm writing a plugin that will allow some of my installs to talk to each other and share specific information. I wish to specify an endpoint such that example.com/path/here is where the other site can GET some nicely formed XML or POST some nicely formed XML (depending on which way the data is flowing).

As a rough hack, I set up ./wp-content/plugins/myplugin/endpoint.php but I get the impression that direct calling is going to be bad. How do I do this the WordPress way?

  • 2
    How about creating a custom REST API endpoint? – Sally CJ Jun 11 '19 at 11:32
  • That might work. How do I do that? – Matthew Brown aka Lord Matt Jun 11 '19 at 11:35
  • 2
    It's all explained in Sally's link. – Jacob Peattie Jun 11 '19 at 11:36
  • 1
    @MatthewBrownakaLordMatt You can send the data as XML in JSON... {"myXml":"xml data here"} ... if it's not easier to just convert the data to JSON.. – Sally CJ Jun 11 '19 at 14:01
  • 1
    I'm coming round to JSON. Conversion is not so hard. – Matthew Brown aka Lord Matt Jun 12 '19 at 7:39
2

You can create a custom endpoint using the add_feed function. I have used this in the past to create custom iCal feeds.

// Initialize the feed
function your_add_custom_feed() {
    // This adds a feed http://example.com/?feed=myfeed
    add_feed('myfeed', 'your_create_feed');
}
add_action('init','your_add_custom_feed');

// Create a function to form the output
function your_create_feed() {

    // Query variables are accessible here ($_GET)
    $myvar = get_query_var('myvar');

    // You may need to specify the header before output here depending on your type of feed
    // header(...)

    // Echo your feed here.

}
|improve this answer|||||
  • 1
    While my use case has changed due to comments, this is exactly the answer to my question as asked. Which is why I have accepted it. I hope it helps others but I would also point the same others to the comments about JSON. – Matthew Brown aka Lord Matt Jun 12 '19 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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