airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.33k stars 110 forks source link

Post/Put ACF Fields not working #196

Open mbaljeetsingh opened 6 years ago

mbaljeetsingh commented 6 years ago

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it.

If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks).

I'm authenticated with JWT Auth Token plugin.

But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
    "title" : "Postman 1",
    "acf" : {
        "short_description": "Again"
    },
    "status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request?

This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

Chris-AdHome commented 6 years ago

You should see POST working using Basic Authentication - I have not tried JWT Auth Token.

Can you get your request working via Postman?

mbaljeetsingh commented 6 years ago

@Chris-AdHome I don't it is a good idea to use Basic Authentication in production.

And no, JWT Token wasn't working in Postman. Basic Auth Works.

airesvsg commented 6 years ago

Hello @mbaljeetsingh, What is the structure of your data to create the task?

{
    "title": "Task 1",
    "fields": {
        "short_description": "Again"
    }
}

Thanks

mbaljeetsingh commented 6 years ago

Hello @airesvsg,

Yes, the structure is like this.

rfenaux commented 6 years ago

Hello @airesvsg , I'm running the latest wordpress version and I have install your plugin (request version 3).

I manage to mage GET request without any problem using both BASIC and token based auth.

However, I am unable to update my custom fields on existing posts. 😢

I'm running the POST/PUT/PATCH request on the following endpoint: /wp-json/acf/v3/posts/the_post_id_to_update

The custom field I'm trying to update is "ai_file" (which returns when I make a GET request)

Here is the response I get when I GET : { "acf": { "ai_file": "sdfsqdfsdfsqdfdsq" } }

When I POST/PATCH/PUT here is the response I get: { "code": "rest_forbidden", "message": "Sorry, you are not allowed to do that.", "data": { "status": 401 } }

Do you think you could help ? 🙏

Kind regards,

rfenaux commented 6 years ago

Alright, I managed to solve my issue:

  1. I had to download the following plugin for some random reason (https://github.com/WP-API/Basic-Auth)
  2. I use PATCH or POST to the following endpoint : http://yoursite.com/wp-json/wp/v2/posts/id with the following json : { "fields": { "your_custom_field": "the_value" } }

NB : the endpoint mentioned in the doc of "ACF to REST API" (/wp-json/acf/v3/posts/id) does not work - it returns a 500 error

ztraboo commented 5 years ago

I was able to successfully use this JWT plugin (https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/) and update the custom acf fields using the "fields" value.

Located the JWT token by issuing http://ew-localhost.com/wp-json/jwt-auth/v1/token POST content-type: application/json; charset=UTF-8 Pass in "username" "password" of Wordpress user to authenticate and receive Bearer token.

Defined a custom post type called courses using this plugin (https://wordpress.org/plugins/custom-post-type-ui/) and allow REST API access.

Example call to update the custom post type course, post id = 91 http://localhost/wp-json/wp/v2/course/91 POST content-type: application/json; charset=UTF-8 authorization: Bearer JWT token goes here

{
    "title": "This is a test title",
    "fields": {
      "course_short_description": "Example short description",
      "content_overview": "Sample content overview",
      "open_edx_meta": {
        "modified_by": [
          3
        ],
        "date_modified": "2018-10-12 23:10:26.139728"
      }
    }
}
killua99 commented 5 years ago

@ztraboo your method did work. I'm now confuse, if we make the request against the WordPress REST API, do we require this pluging at all ?

Answer: Yes, we do require the plugin, since it add the ability to update and get ACF fields from the normal WP V2 REST API.

So, install it, but use the WP V2 REST API instead

ztraboo commented 5 years ago

@killua99 I'm using the JWT token authentication instead of Basic Authentication (Wordpress user/pass) to authenticate a different way with the WP V2 REST API. You don't have to use the JWT plugin to authenticate but can instead use Basic Authentication if you want to. I just chose to use JWT for my application. That way if someone were to crack my user credentials using JWT they wouldn't have access to the Wordpress admin page but if they had Basic Authentication they would be able to do that.

netsenior commented 5 years ago

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it.

If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks).

I'm authenticated with JWT Auth Token plugin.

But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
  "title" : "Postman 1",
  "acf" : {
      "short_description": "Again"
  },
  "status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request?

This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

in json above use "fields" instead of "acf"

ztraboo commented 5 years ago

@netsenior The documentation for ACF says to use "acf" setting but if you look up at my example here https://github.com/airesvsg/acf-to-rest-api/issues/196#issuecomment-430637497 you'll see that I'm using "fields" instead. Change that name to see what it does in your POST body JSON payload.

airesvsg commented 5 years ago

Use the filter acf/rest_api/key to change the key fields.

add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
    return 'acf_fields';
}, 10, 3 );

Now, the fields should be sent into the key acf_fields.

okta2510 commented 5 years ago

Hi,

wordpress version: 4.9.1, plugin version: 3.1.0

I have created custom field type called Tasks And added a ACF field short_description to it. If i add/ update the short_description in the tasks from wp-admin, I can see the result with short_description (ACF field) at the endpoint (wp-json/wp/v2/tasks). I'm authenticated with JWT Auth Token plugin. But the problem happens, I can create/ update task (using POSTMAN)

(POST) http://localhost/wordpress/wp-json/wp/v2/tasks/19

{
    "title" : "Postman 1",
    "acf" : {
        "short_description": "Again"
    },
    "status" : "publish"
}

It creates the Task, but with title only. Am I passing the ACF fields the right way to POST/ PUT request? This is the one thing that is stopping me from using Wordpress as a complete API backend for any application.

in json above use "fields" instead of "acf"

yess buddy you help me on this, thanks God bless!

P4ul2000 commented 5 years ago

Hi,

I have put:

add_filter( 'acf/rest_api/key', function( $key, $request, $type ) {
  return 'acf_fields';
}, 10, 3 );

in my theme function file, but I am still getting:

 "acf": {
        "paul": null,
        "custom": null
    },

acf and not acf_fields

Please could someone let me know how to fix? Thank you!

I have the latest version of wordpress and acf installed.

ProtocolNebula commented 3 years ago

Guys, I have the same issue, seems that here is solved (I'm not sure if this video is in English too): https://www.miwordpress.es/%E2%9C%85-acf-en-el-api-de-wordpress-%F0%9F%9A%80%F0%9F%9A%80%F0%9F%9A%80/

The plugin that uses in the video: https://github.com/airesvsg/acf-to-rest-api/