Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active February 25, 2022 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhowellsmead/f39447a7c933ffb2f3bc7f390c51e3b4 to your computer and use it in GitHub Desktop.
Save markhowellsmead/f39447a7c933ffb2f3bc7f390c51e3b4 to your computer and use it in GitHub Desktop.
Creates a post on a remote WordPress site, using the REST API and an application password
const remote_domain = 'https://example.org', // without trailing slash
application_password = 'Application password',
username = 'remote_user_name';
const body = JSON.stringify({
title: 'Post using REST API',
content: 'Post content using REST API',
status: 'publish'
});
const options = {
cache: 'no-cache',
mode: 'cors',
credentials: 'include',
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(username + ':' + application_password)
}),
body: body
};
fetch(`${remote_domain}/wp-json/wp/v2/posts`, options)
.then(data => console.log(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment