wordpress
Wordpress is a web publishing platform, which allows you to add and edit content to your website. This can be done by visiting the administration interface, wp-admin. However, there is also an XML RPC programming interface that allows you to do the same from any programming language that supports this interface.

In this short article I will show how to add new posts to WordPress from Python. There are several libraries available that allow you to view and edit content, but they were all inappropriate for me. I wanted to be able to convert my old website into WordPress posts and have the published date set to the date I originally posted items on my old site. However, every library I tried either didn’t have the possibility to set the published date, or WordPress didn’t understand it.

Here’s the short piece of code that allowed me to add new posts to WordPress, without the use of any 3rd party library:

import datetime, xmlrpclib

wp_url = "http://www.example.com/xmlrpc.php"
wp_username = "someuser"
wp_password = "secret"
wp_blogid = ""

status_draft = 0
status_published = 1

server = xmlrpclib.ServerProxy(wp_url)

title = "Title with spaces"
content = "Body with lots of content"
date_created = xmlrpclib.DateTime(datetime.datetime.strptime("2009-10-20 21:08", "%Y-%m-%d %H:%M"))
categories = ["somecategory"]
tags = ["sometag", "othertag"]
data = {'title': title, 'description': content, 'dateCreated': date_created, 'categories': categories, 'mt_keywords': tags}

post_id = server.metaWeblog.newPost(wp_blogid, wp_username, wp_password, data, status_published)
Using Python to add new posts in WordPress
Tagged on:     

19 thoughts on “Using Python to add new posts in WordPress

Leave a Reply

Your email address will not be published.