Skip to content

Instantly share code, notes, and snippets.

@basilleaf
Last active December 18, 2015 19:49
Show Gist options
  • Save basilleaf/5835620 to your computer and use it in GitHub Desktop.
Save basilleaf/5835620 to your computer and use it in GitHub Desktop.
post to wordpress from python
# pip install python-wordpress-xmlrpc
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
def post_to_wordpress(title, content, more_info_url, local_img_file):
# first upload the image
data = {
'name': local_img_file.split('/')[-1],
'type': 'image/jpg', # mimetype
}
wp = Client('http://www.marsfromspace.com/xmlrpc.php', WP_USER, WP_PW)
# read the binary file and let the XMLRPC library encode it into base64
with open(local_img_file, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
attachment_id = response['id']
# now post the post and the image
post = WordPressPost()
post.post_type = 'portfolio' # stupid effing theme
post.title = title
post.content = content
post.post_status = 'publish'
post.thumbnail = attachment_id
wp.call(NewPost(post))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment