eric buth

Simple WordPress Posting from Ruby via XMLRPC

WordPress supports a few different XMLRPC APIs, all of which have different methods and key titles for specifying content. For a project I’m working on I was looking for the simplest Ruby code to get a post up with a title, content, tags, and categories.

The code bellow is a mashup of the available options that seems to work just fine.

require 'xmlrpc/client'

# build a post

post = {
  'title'       => 'Post Title',
  'description' => 'The content of the post',
  'mt_keywords' => ['a', 'list', 'of', 'tags'],
  'categories'  => ['a', 'list', 'of', 'categories']
}

# initialize the connection

connection = XMLRPC::Client.new('yourdomain.com', '/xmlrpc.php')

# make the call to publish a new post

connection.call(
  'metaWeblog.newPost',
  1,
  'wp_username',
  'wp_password',
  post,
  true
)

I was a little surprised by how not straight forward this was, but, like I said, this end result works.