1

I am trying to post to WordPress using the REST API. The aim is to have a form that accepts The following information:

  • Title
  • Content
  • ACF Custom Field 1 (possibly repeater field)
  • ACF Custom Field 2 (possibly repeater field)
  • Featured Image

I am new to the WP API and am having some difficulty finding some solid documentation on how I could post to a WP site from an external page/site that is not WordPress.

The idea is that users of website 'a' can fill out a form and this create a post on website 'b'.

I am not sure on user authentication as of yet so will either post annonymously or with a single user account.

Is anyone able to show me a simple method showing a basic outline of the above using Javascript or point me in the right direction atleast as I have found the docs a little confusing.

2 Answers 2

1

To answer your broader question about resources - while there are many articles out there, I would always recommend checking out the official WordPress API Handbook first.

You first have to decide which JavaScript library you want to use to make HTTP requests to access the REST API. Axios is one example.

To set the featured image, you first have to uploaded it as media (which you can do via the REST API) and then include the media id in the post request (more details below).

Then to create the post via the REST API, you would make a POST HTTP request to https:/<your-site>/wp-json/wp/v2/posts with a body like:

{
    content: 'My post content here',
    title: 'Post Title',
    featured_media: [image-media-id],
    status: 'publish' // or 'draft'
}

To get the ACF Custom Fields accessible/editable via the REST API, check out this plugin: https://github.com/airesvsg/acf-to-rest-api/. I haven't used it yet, but it seems like what you're looking for.

Also be aware that if site 'a' is on a different domain than site 'b', you may have CORS issues to deal with as well. WordPress has good guides for dealing with that.

-1

I think the simplest solution, without having more details about site A or site B, would be to use a service like Zapier to make your connection.

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.