Last active
December 6, 2023 13:34
-
-
Save arashtad/d4d47828ee7bb7a9b490c05ee923297a to your computer and use it in GitHub Desktop.
How to Upload Files on WordPress Websites Using Python and WordPress API
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
''' | |
▄▄▄ ██▀███ ▄▄▄ ██████ ██░ ██ ▄▄▄█████▓ ▄▄▄ ▓█████▄ | |
▒████▄ ▓██ ▒ ██▒▒████▄ ▒██ ▒ ▓██░ ██▒▓ ██▒ ▓▒▒████▄ ▒██▀ ██▌ | |
▒██ ▀█▄ ▓██ ░▄█ ▒▒██ ▀█▄ ░ ▓██▄ ▒██▀▀██░▒ ▓██░ ▒░▒██ ▀█▄ ░██ █▌ | |
░██▄▄▄▄██ ▒██▀▀█▄ ░██▄▄▄▄██ ▒ ██▒░▓█ ░██ ░ ▓██▓ ░ ░██▄▄▄▄██ ░▓█▄ ▌ | |
▓█ ▓██▒░██▓ ▒██▒ ▓█ ▓██▒▒██████▒▒░▓█▒░██▓ ▒██▒ ░ ▓█ ▓██▒░▒████▓ | |
▒▒ ▓▒█░░ ▒▓ ░▒▓░ ▒▒ ▓▒█░▒ ▒▓▒ ▒ ░ ▒ ░░▒░▒ ▒ ░░ ▒▒ ▓▒█░ ▒▒▓ ▒ | |
▒ ▒▒ ░ ░▒ ░ ▒░ ▒ ▒▒ ░░ ░▒ ░ ░ ▒ ░▒░ ░ ░ ▒ ▒▒ ░ ░ ▒ ▒ | |
░ ▒ ░░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░ ░ ░ ▒ ░ ░ ░ | |
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ | |
░ | |
This gist describes a ways to develop a script that uploads files on WP. | |
Usable for different types of files such as images, PDF, PPTX, etc. | |
Fully tested on API v2 | |
''' | |
### upload.py | |
import os | |
import base64 | |
import requests | |
from credentials import get_wp_access | |
def upload_file(relative_path, caption, destination): | |
# Get the absolute path to the directory containing this script | |
project_dir = os.path.dirname(os.path.abspath(__file__)) | |
# Combine the project directory with the relative path to get the full system path | |
file_path = os.path.join(project_dir, relative_path) | |
api_url, username, password = get_wp_access(destination) | |
credentials = username + ':' + password | |
token = base64.b64encode(credentials.encode()) | |
json_header = {'Authorization': 'Basic ' + token.decode('utf-8')} | |
media = {'file': open(file_path, "rb"), 'caption': caption} | |
response = requests.post(api_url + "media", headers=json_header, files=media) | |
print(response.text) | |
# Example usage with a relative path within the project directory | |
upload_file('relational/path/to/file.pptx', 'Sample caption', 'site_1') | |
### credentials.py | |
destinations = { | |
"site_1": { | |
"api_url": "https://example-1.com/wp-json/wp/v2/", | |
"username": "wp_username", | |
"password": "XXXX XXXX XXXX XXXX XXXX XXXX" | |
}, | |
"site_2": { | |
"api_url": "https://example-2.com/wp-json/wp/v2/", | |
"username": "wp_username", | |
"password": "XXXX XXXX XXXX XXXX XXXX XXXX" | |
}, | |
"site_3": { | |
"api_url": "https://example-3.com/wp-json/wp/v2/", | |
"username": "wp_username", | |
"password": "XXXX XXXX XXXX XXXX XXXX XXXX" | |
}, | |
"site_4": { | |
"api_url": "https://example-4.com/wp-json/wp/v2/", | |
"username": "wp_username", | |
"password": "XXXX XXXX XXXX XXXX XXXX XXXX" | |
}, | |
"site_5": { | |
"api_url": "https://example-5.com/wp-json/wp/v2/", | |
"username": "wp_username", | |
"password": "XXXX XXXX XXXX XXXX XXXX XXXX" | |
}, | |
# More websites if needed... | |
} | |
def get_wp_access(destination): | |
access_info = destinations.get(destination, {}) | |
api_url = access_info.get("api_url", "") | |
username = access_info.get("username", "") | |
password = access_info.get("password", "") | |
return api_url, username, password | |
''' | |
https://arashtad.com | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment