Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Created April 24, 2020 12:05
Show Gist options
  • Save openroomxyz/73359be882d2ee22c761a9e24a060b78 to your computer and use it in GitHub Desktop.
Save openroomxyz/73359be882d2ee22c761a9e24a060b78 to your computer and use it in GitHub Desktop.
Python WordPressAPI : How to create a post?
import base64, requests, json
def header(user, password):
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header_json = {'Authorization': 'Basic ' + token.decode('utf-8')}
return header_json
def create_post(content,title, url, header_json):
post = {
'title' : title,
'status' : 'publish',
'content' : content,
'categories': 5, # category ID
'date' : '2020-01-05T10:00:00',
}
responce = requests.post(url + "wp-json/wp/v2/posts/" , headers=header_json, json=post)
print(responce.text)
print(json.loads(responce.content)['link'])
header_json = header("admin","XXXX XXXX XXXX XXXX XXXX XXXX")
create_post('PIKIJAKOB Rad pijem kawic','Title the piple','https://X.xyz/XXX/', header_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment