Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Created April 18, 2023 03:19
Show Gist options
  • Save MonteLogic/bb3e5286f4fea1378f1be1a533cdf3da to your computer and use it in GitHub Desktop.
Save MonteLogic/bb3e5286f4fea1378f1be1a533cdf3da to your computer and use it in GitHub Desktop.
function getThisToWork() {
fetch('/wp-json/wp/v2/users/3', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': wpApiSettings.nonce, // replace with your nonce value
},
})
.then(response => response.json())
.then(data => {
console.log(data)
let incomingValue = JSON.parse(data.meta.list_of_associated_campaigns);
let outGoingValue;
console.log(incomingValue);
console.log(typeof incomingValue);
console.log(data);
if( Array.isArray(incomingValue)){
incomingValue.push(post_ID.value);
outGoingValue = incomingValue;
}
if (!Array.isArray(incomingValue) && incomingValue != undefined) {
outGoingValue = Array(1).fill(incomingValue);
console.log(outGoingValue);
}
if (incomingValue == "" || incomingValue == undefined) {
console.log("Does not exist")
let value = "New Value";
outGoingValue = Array(1).fill(value);
console.log(array);
}
if (incomingValue != "" && incomingValue != undefined) {
data.meta.list_of_associated_campaigns = incomingValue;
console.log(incomingValue)
let array = Array(1).fill("New Value");
console.log("Does exist")
}
console.log(incomingValue);
const outGoingObject = JSON.stringify({
meta: {
list_of_associated_campaigns: JSON.stringify(outGoingValue)
}
});
fetch('/wp-json/wp/v2/users/3', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': wpApiSettings.nonce, // replace with your nonce value
},
body: outGoingObject,
}).then(response => response.json())
.then(data => console.log(data))
})
}
getThisToWork();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment