3

I dont want to use a REST API to publish to wordpress.

I want to create a form on a wordpress page which will send a POST to an external REST API. Users will put in their information, and perhaps upload a file. When the press "submit" I want this information to be sent to a REST API on a different server (not wordpress).

Are there any plugins that allow this? Has anyone done this? Of course, I might run into troubles with this: http://en.wikipedia.org/wiki/Same_origin_policy

It looks like I want to do the reverse of this: http://wordpress.org/extend/plugins/json-api/ Instead of making a REST API for wordpress, I want to make wordpress communicate send a POST to an external REST API when a user presses a button on a form.

1 Answer 1

4

I don't know of a plugin that does it in a general way; for the most part, you'll need to build something custom for each specific API you intend to communicate with.

For your purposes, the key function will be wp_remote_post(), which is a wrapper for the POST method of WP's HTTP class. (Use this instead of making manual cURL requests, because WP_Http has all kinds of fallbacks for different modes of HTTP transport.) Here's a nice introduction: http://yoast.com/wp-best-practice/wordpress-http-api/ Same origin policies generally only apply to browsers. wp_remote_post() and its ilk are fired on the server.

Sending data is pretty easy - just put an array into the 'body' of your wp_remote_post() call. The tricky part is always authentication - does the API server expect a pre-registered key, or some sort of oAuth handoff, or what. If it's as simple as a shared key, you can just pass it as part of the 'body' payload.

4
  • 1
    This page has been intentionally deleted. too bad that you didn't write an example so it can keep in time Commented Nov 29, 2019 at 4:12
  • 2
    Does anyone could helps on this? I m looking at it too. to modify wpForm submit button post to external API Commented Jun 10, 2020 at 4:12
  • There is documentation on wp_remote_post on WP's website, but I don't think it's enough. It's a start. developer.wordpress.org/reference/functions/wp_remote_post Commented Aug 11, 2021 at 21:02
  • There's also this wordpress.stackexchange question and answers: wordpress.stackexchange.com/questions/49022/… Commented Aug 11, 2021 at 21:07

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.