i want to update my acf field present in a page using REST API, how can i do that, it is very difficult to me as i searched everywhere but didnt get my answer
1 Answer
I had the same problem and sticked together this javascript-code from various sources, which works great now:
const username = 'your_wp_username'
const pass = 'your_wp_appliaction_password'
const token = window.btoa(`${username}:${pass}`)
const url = 'https://yoururl.com/wp-json/acf/v3/projects/384?fields[title]=Better title';
const params = {
'cache': 'no-cache',
'method': 'PATCH',
'credentials': 'omit',
'headers': {
'Authorization': `Basic ${token}`,
'Content-Type': 'application/json; charset=UTF-8; application/x-www-form-urlencoded',
},
}
fetch(url,params)
.then((res) => {
return res.json();
})
.then((data) => {
console.log(data)
})
Make sure to replace the following variables in my code:
your_wp_username
You can find your wordpress-username in: WP-Backend --> User --> Edit
your_wp_application_password
You can generate an Application Specific Password in:
WP-Backend --> User --> Edit --> Scroll to the bottom or by following this guide. The password will look something like this: XXXX XXXX XXXX XXXX XXXX XXXX
your_url
- Make sure to change the url
https://yoururl.com/wp-json/acf/v3/projects/to your custom API endpoint. - The number
384after the endpoint is the ID of the element / post we're updating in this request - The word
titlein the url is the fieldname we're updating