Created
April 24, 2020 12:05
-
-
Save openroomxyz/73359be882d2ee22c761a9e24a060b78 to your computer and use it in GitHub Desktop.
Python WordPressAPI : How to create a post?
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
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