Nov 112009
 

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)

  10 Responses to “Using Python to add new posts in WordPress”

  1. Is there any python script for posting or receiving comments on a blog?

    How to post comments using XML RPC?

    • I haven’t looked into that. I would imagine it being used as a spam tool, so perhaps it isn’t even possible by default in WordPress.

  2. Thank you for posting this – was having bunches of trouble getting a custom date_created to be accepted

    took your code and used it for date_created_gmt and got it working

    thank you again

  3. Thanks so much for this! It’s really useful because I’ve been trying to run scripts to schedule posts automatically.

    It looks like you can’t create new categories on the fly just by supplying the array with new items. If you want to know what categories are already available for you to use, you can use this code (building off of the other code):

    categories = server.metaWeblog.getCategories(wp_blogid, wp_username, wp_password)

    for item in categories:
    print item['description']

    (Remember to indent the print statement.)

  4. [...] seems wordpresslib, but I found a code that requires no such a library at “Using Python to add new posts in WordPress“. import datetime, [...]

  5. Extremely helpful! Thanks!!!

  6. [...] There seems wordpresslib, but I found a code that requires no such a library at “Using Python to add new posts in WordPress“. [...]

  7. [...] Mercurial distributed version control system, thanks to this amazing post by someone who needed to publish to WordPress from a Python script. Add to that, a few program modifications and a little bit of Linux command-line piping, and [...]

  8. Hello thank you for your post…How about updating an existing post?
    I need code

  9. Thank you so much for that great article. I were looking for a solution to generate automatic postings on a testing blog. I just tried your solution and it worked!!!!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>