I'm new working with WordPress
and I'm playing around with the Advanced Custom Fields
plugin. It seems nice but I'd like to know if it's possible to POST
a new object (created using ACF
) through the WordPress REST API
? I'm already using it to GET
all my custom objects (thanks to ACF to REST API Plugin
).
I'm using WordPress
as my backend and NextJS
as the frontend so I'd like to create a new HTML form, where the user can input some info and directly create an instance of that object.
If it's not possible, what's the mechanism to save to the database (common MySQL instance) and simulate the same operation I need? I'd like to prevent going through some custom implementation if it's something out there yet (let me know if you need more info about the problem or the data)
EDIT:
After some research, I figured out that I was trying to create the object using the incorrect endpoint.
Now I'm able to create my own object (custom post type) but I'm not able to populate the ACF fields...
I send an standard request:
var data = JSON.stringify({
"title": "Test00",
"status": "publish",
"acf":{
"customfield1":"Some value..."
}
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8000/wp-json/wp/v2/custom");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMCIsImlhdCI6MTU2MDM3NTQxNywibmJmIjoxNTYwMzc1NDE3LCJleHAiOjE1NjA5ODAyMTcsImRhdGEiOnsidXNlciI6eyJpZCI6IjIifX19.BCyrlFm_qD3-9DzCxQ37n4pJYkTasvLaN34NJtFAMC4");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
And I receive this:
{
...
"acf":{
"customfield1":null
}
}
Is there any way to do it all at once? Should I create the object and then, send the extra info?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…