Skip to content

Instantly share code, notes, and snippets.

@openroomxyz
Last active December 23, 2024 21:37
Show Gist options
  • Save openroomxyz/f32021d077d1be5235ceb1a716d1e37a to your computer and use it in GitHub Desktop.
Save openroomxyz/f32021d077d1be5235ceb1a716d1e37a to your computer and use it in GitHub Desktop.
Python WordPress API : How to upload image to wordpress?
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 upload_image_to_wordpress(file_path, url, header_json):
media = {'file': open(file_path,"rb"),'caption': 'My great demo picture'}
responce = requests.post(url + "wp-json/wp/v2/media", headers = header_json, files = media)
print(responce.text)
hed = header("admin","XXXX XXXX XXXX XXXX XXXX XXXX") #username, application password
upload_image_to_wordpress('C://Users//X//Desktop//X//X.png', 'https://XXX.xyz/XXX/',hed)
@florian-glombik
Copy link

florian-glombik commented Aug 4, 2024

How can I add title ?

@mzakariabigdata You can find the documentation of the API here https://developer.wordpress.org/rest-api/reference/media/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment