Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khai-nguyen-dinh/d1e852ef35ed8b978f0a56a3087a47d4 to your computer and use it in GitHub Desktop.
Save khai-nguyen-dinh/d1e852ef35ed8b978f0a56a3087a47d4 to your computer and use it in GitHub Desktop.
import requests
import base64
import random
import urllib.request
def download_image(url):
name = random.randrange(10000000, 100000000)
fullname = str(name) + ".jpg"
urllib.request.urlretrieve(url, fullname)
return fullname
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(image, url, header_json):
fileName = download_image(image)
print(fileName)
media = {'file': open('./' + fileName, "rb"), 'caption': 'My great demo picture'}
responce = requests.post(url + "wp-json/wp/v2/media", headers=header_json, files=media)
newDict = responce.json()
print(newDict['guid']['raw'])
hed = header("username", "Application Password")
upload_image_to_wordpress('http://image/url.jpg',
'https://wordpress-domain.com/', hed)
@khai-nguyen-dinh
Copy link
Author

Script auto download image from image url and upload to wordpress using python and wordpress api. To use this script please follow steps bellow:

  1. Create application user on wordpress admin
  2. Update new account information and url on this file.
  3. Enjoy!!

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