0

I would like to put out a form on the front end of wordpress, and be able to post the data into Advanced Custom Fields. However, it seems I only have access to 'title' and 'content'. Am I able to target the custom fields from JS file specifically?

Below code line 5, I tried using "getField('chapter'): $(".chapter").val(),", but it doesn't work.

postNote(e) {
    var ournewPost ={
        'title': $(".new-note-title").val(),
        'content': $(".new-note-body").val(),
        getField('chapter'): $(".chapter").val(),
        'status': 'publish'
    }
    $.ajax({
        beforeSend: (xhr) => {
            xhr.setRequestHeader('X-WP-Nonce', Data.nonce);
        },
        url: Data.root_url + '/wp-json/wp/v2/note/',
        type: 'POST',
        data: newNote,
    });
}
Cowie
  • 1
  • 2

1 Answers1

0

You need to expose the endpoints in Wordpress Rest API for your advanced custom fields. There is a plugin for this: https://wordpress.org/plugins/acf-to-rest-api/ * (see end of answer for alternative)

You then can have a look with a RESTClient (Postman or something similar) and make sure that the field is in your response: https://www.yourwordpressurl.com/wp-json/wp/v2/note/

If you can find your field, you will also see how to access it by the name and path (you easily will find content and title you are already using. It may be inside of a acf: I guess, you need to have a look what the structure looks like.

Things can just be chained up:

['wp:featuredmedia']['0'].media_details.sizes.medium_large.source_url

for getting the url of your medium sized featured image. It depens on your field:

[acf] ....

With this scheme you should be able to find the right way to access your data.


*If you like to add your endpoint programmatically, there is a helpful question here on SO: Adding ACF to CUSTOM wp api endpoints

rank
  • 2,361
  • 2
  • 10
  • 20