Post to WordPress with Ruby

While this post still describes a valid way of posting to WordPress through ruby, you might want to check this more recent article out.


Digging around some old bits of code, I found a little script I knocked back ages ago, that would allow me to publish posts and categories to a WordPress Blog in a programmatic manner, using the AtomPub protocol.

I ended up using a couple more tricks, that enabled me to automate switching Atom Publishing on on a WordPress blog, and publish posts that belonged to categories that did not already exist in the blog’s database.

Mechanize was and is my weapon of choice when it comes to web automation, and over the years I have used it time and again with great success. In this context, I used it to log in the blog, switch AtomPub on, and also to create categories that did not exist, as the wordpress service would simply ignore them, if you tried to specify them in the blog entry.

I also used good ole net/http and Nokogiri (required by Mechanize in any case) to fetch and parse the category list, in the category_exists? method.

In this version, I used ratom as the base Atom client, but later went on and replaced it with my own extension that additionally uses simple heuristics to add a to_atom method to models.

This is by no means production ready, but is nevertheless a nice bit of kit to play with, and get you on your way to WordPress Automation.

I will eventually publish the ruby gem we are using internally to remotely manage WordPress blogs, but I do not feel it is yet generic enough to be of use to the community.

This entry was posted in Code and tagged , , , , . Bookmark the permalink.

7 Responses to Post to WordPress with Ruby

  1. Roland Mai says:

    Thanks for writing this up. I found one bug with the category bit, you need to change it to:
    post.categories << Atom::Category.new(post_cat)

  2. Thanks for your post! Can I use this code to create a Ruby crawler to post some content on WordPress?

  3. Chris says:

    seem to be running into the following when trying the above. any ideas?


    /usr/lib/ruby/gems/1.9.1/gems/ratom-0.6.10/lib/atom/pub.rb:170:in `publish': Invalid response: # (Atom::Pub::ProtocolError)
    from test.rb:44:in `publish_post'

  4. Chris says:

    ah seems my previous response got cut off just a bit. I’ll put it in a gist. https://gist.github.com/9a7341fe3c2fe1e3b514

    • ac says:

      Hey Chris, looks like you are posting to the wrong URL or something, the error you re getting is a Net::HTTPNotFound error. As far as I know WordPress has not changed its admin URLs recently, so you probably have a typo in your domain or something. Anyways, you might also want to check out this other approach, which is more stable than this one.

Leave a Reply

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

*

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>