Skip to content

Instantly share code, notes, and snippets.

@Komock
Created May 18, 2018 07:14
Show Gist options
  • Select an option

  • Save Komock/8e15e4be228a92f6ff7529ba33d84254 to your computer and use it in GitHub Desktop.

Select an option

Save Komock/8e15e4be228a92f6ff7529ba33d84254 to your computer and use it in GitHub Desktop.
Submit form with wp-nonce token using fetch
const formEl = document.querySelector('.form-profile-upd');
formEl.addEventListener('submit', e => {
e.preventDefault();
const data = new FormData(formEl);
const id = window.WP_DATA.USER_ID;
console.log(data.get('birthday'));
fetch(`${WP_DATA.ROOT_URL}wp/v2/users/` + id, {
method: 'POST',
credentials: 'same-origin',
body: {
'meta': {
'birthday': data.get('birthday')
}
},
headers: {
'X-WP-Nonce': WP_DATA.WP_NONCE
}
})
.then(res => res.text())
.then(data => console.log(data));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment