Using Python to add new posts in WordPress

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 thoughts on “Using Python to add new posts in WordPress

  1. Permalink  ⋅ Reply

    Harshad Joshi

    June 12, 2010 at 7:31am

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

    How to post comments using XML RPC?

    • Permalink  ⋅ Reply

      jansipke

      June 12, 2010 at 7:49am

      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. Permalink  ⋅ Reply

    Alvin

    January 9, 2011 at 7:20pm

    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. Permalink  ⋅ Reply

    Garage Flooring

    June 24, 2011 at 8:54pm

    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. Permalink  ⋅ Reply

    kastechno

    November 27, 2012 at 6:49pm

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

  5. Permalink  ⋅ Reply

    rene

    February 14, 2013 at 12:00pm

    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

Your email will not be published. Name and Email fields are 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>