
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.
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)
Thanks for noting that Roland, I patched the gist.
Thanks for your post! Can I use this code to create a Ruby crawler to post some content on WordPress?
Not sure what you mean by ‘crawler’, but you can definitely post to wordpress using this. Check out this alternative though, it’s an updated version of this post, using the WordPress JSON API plugin.
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'
ah seems my previous response got cut off just a bit. I’ll put it in a gist. https://gist.github.com/9a7341fe3c2fe1e3b514
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.