Last active
February 25, 2022 08:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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