WordPress Book Posting Scripts

Module 4: wp_poster.py (Posting to WordPress)

I decided to get a bit fancier for sending the posts to wordpress and create a class. That way I could initialize it with the url, username, and password and just keep calling the class instance to send additional posts to the site.

I also wanted to be able to save keywords for tags. I have the script scan the content for keywords, and if they are found then it adds a specific tag to the post. I used this to create my character tags – if a character’s name or nickname is found in a post, then they are added to the tags for that post.

Surprisingly, there are some chapters that don’t have Abby’s name in them, even though she’s the point of view character – so I included a dictionary of tag aliases and set it up so that Abby would be added as a tag if the script found “Abby”, “Abbigail”, or “I”.

import xmlrpclib

class WP_Poster(object):
    def __init__(self, wp_url, wp_username,
                 wp_password, wp_blogid=""):
        assert wp_url
        assert wp_username
        assert wp_password
        self.wp_username = wp_username
        self.wp_password = wp_password
        self.wp_blogid = wp_blogid
        self.auto_tags = None
        self.tag_aliases = None
        self.server = xmlrpclib.ServerProxy(wp_url)

    def set_potential_tags(self, potential_tags, tag_aliases):
        self.auto_tags = [x.upper()
                          for x in potential_tags]
        self.tag_aliases = tag_aliases

    def post_item(self, title, content,
                  categories, status_published=1,
        if tags is None:
            tags = []
        if self.auto_tags:
            temp_content = content.upper()
            for x in ".,'\";-":
                temp_content = temp_content.replace(x, " ")
            temp_content = temp_content.split()
            tags.extend([x.title() for x in self.auto_tags
                    if x.upper() in temp_content])
            if self.tag_aliases:
                    tags.extend([tag.title() for (alias, tag)
                                 in self.tag_aliases.iteritems()
                                 if alias.upper() in temp_content])

        data = {'title': title,
                'description': content,
                'dateCreated': date_created, 'categories': categories,
                'mt_keywords': tags}
        post_id = self.server.metaWeblog.newPost(
        print "Posted: %s (%s)" % (title, tags)
        return post_id

if __name__ == '__main__':

    def main():
        # Test creating the WP_Poster
        wp_url = "http://YourWordPressSite/xmlrpc.php"
        wp_username = "YourUsername"
        wp_password = "YourPassword"
        wp_blogid = ""

        wp_poster = WP_Poster(

        # Test creating a post
        import datetime
        title = "Title with spaces-Postertest"
        content = "Body with lots of content"
        date_created = xmlrpclib.DateTime(datetime.datetime.strptime("2009-10-20 21:08", "%Y-%m-%d %H:%M"))
        print date_created
        now = datetime.datetime.now()
        print now
        date_created = xmlrpclib.DateTime(datetime.datetime.strptime(datetime.datetime.strftime(now, "%Y-%m-%d %H:%M"), "%Y-%m-%d %H:%M"))
        categories = ["Writing Update"]
                            title, content,
                            categories, status_published=1,

For this I had to reference Jan Spike van der Veen’s blog post on using python to post to wordpress.

Utility Scripts , ,

Leave a Reply

Your email address will not be published. Required fields are marked *