Last active
May 16, 2025 23:47
-
-
Save florian-glombik/c2e71fa2128fa1a69bb39aa61fadf3bd to your computer and use it in GitHub Desktop.
Upload file to Wordpress using REST API (python)
This file contains hidden or 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, logging | |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') | |
# This solution was inspired by an existing script https://gist.github.com/openroomxyz/f32021d077d1be5235ceb1a716d1e37a | |
# This script includes further documentation and explanation than the solution by @openroomxyz | |
### -------- VALUES TO BE MODIFIED (start) -------- ### | |
URL = "https://mydomain.com" | |
USERNAME = "admin" | |
# Important: The application password is not the same as the login password (the username is) | |
# To create an application password, go to wp-admin -> Users -> Edit User | |
# See documentation on the application passwords: | |
# https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#basic-authentication-with-application-passwords | |
APPLICATION_PASSWORD = "XXXX XXXX XXXX XXXX XXXX XXXX" | |
path_to_file_to_be_uploaded = "C://Users//X//Desktop//X//X.pdf" | |
### -------- VALUES TO BE MODIFIED (end) -------- ### | |
def create_header(username, application_password): | |
credentials = username + ':' + application_password | |
token = base64.b64encode(credentials.encode()) | |
header = {'Authorization': 'Basic ' + token.decode('utf-8')} | |
return header | |
def upload_file_to_wordpress(file_path): | |
logging.info(f"Uploading file to WordPress ({URL}): {file_path}") | |
media = {'file': open(file_path, "rb")} | |
# Documentation of the API: https://developer.wordpress.org/rest-api/reference/media/ | |
response = requests.post(URL + "/wp-json/wp/v2/media", headers=create_header(USERNAME, APPLICATION_PASSWORD), | |
files=media) | |
if response.status_code == 201: | |
logging.info(f"File uploaded successfully: {response.json()['link']}") | |
else: | |
logging.error(f"Error uploading file (status: {response.status_code}): {response.text}") | |
if __name__ == '__main__': | |
upload_file_to_wordpress(path_to_file_to_be_uploaded) |
No - I did not have that problem. From the error message I would assume that your user just does not have sufficient rights (so not the correct role within Wordpress)
I guess the your user will need the role Administrator
/ Editor
/ Author
to upload files, and as mentioned: it is important that you create an application password
which is not the same as the one with which you usually log in to the admin/editing interface
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, looks nice, were you having this error when developing:
Error uploading file (status: 401): {"code":"rest_cannot_create","message":"Lo siento, no tienes permisos con este usuario para crear entradas. ","data":{"status":401}}
Thanks.