[HowTo] Publish Post Via XML-RPC In WordPress

There are many ways to publish a WordPress blog post. You can publish from the WordPress admin interface, you can publish via email and also you can publish it via XML-RPC. So what is XML-RPC? XML-RPC is a remote procedure call (RPC) protocol which uses XML to encode its calls and HTTP as a transport mechanism. Which means we can call the WordPress procedure remotely. And this will make WordPress easily integrate with other systems.

To enable publish via XML-RPC in WordPress follow these steps:

  1. Log in to your WordPress admin
  2. Go to Settings > Writing > Remote Publishing
  3. Enable the “Enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols” checkbox.

To try it you can use this code below:

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
}

To test it:

$title="Lorem ipsum";
$body="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vestibulum pharetra mi quis rhoncus. Mauris lacinia neque id lacus lobortis a dictum augue molestie. Proin ut elit velit, in faucibus neque. Aliquam libero odio, bibendum non tempor eu, sodales vel felis.";

$rpcurl="http://your_wordpress_blog_url/xmlrpc.php";
$username="Wordpress admin username here";
$password="Wordpress admin password here";
$categories="category name here";

echo wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories,'');

With this XML-RPC it is possible for you to write articles offline and then publish them with XML-RPC. Here are some good tools for that:

  1. Windows Live Writer (Windows Only)
  2. BlogJet (Windows Only)
  3. Blogo (Mac Only)
  4. MarsEdit (Mac Only)
  5. BloGTK (Linux Only)
  6. Scribefire (Firefox Add-on / All platform)
  7. Flock (Web Browser / All platform)