[HowTo] Publish Post Via XML-RPC In WordPress

There are many ways to publish 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 mean we can call the WordPress procedure remotely. And this will make WordPress easily integrate with other system.

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

  1. Login to your WordPress admin
  2. Go to Settings > Writing > Remote Publishing
  3. Enable 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 article offline and then publish it with XML-RPC. Here 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)

Comments

  1. LeoGanda says:

    van, klo pakai .net ada ga?? mau bikin something nih..

  2. Anonymous says:

    Hi,
    thanks for this trick, I will try it as soon as I get a chance. In the meantime, two questions:
    1) does it work with https, to not send the password in clear over the net?
    2) is this "future-proof"? I mean, how much is it likely that this function won't work anymore in future versions of wordpress? Reason I'm asking is I'm evaluating wordpress for my next website, and ability to update it from the command line may be what makes the difference, but only if I'm sure this will continue to be available.
    Thanks,
    Marco

    • Hi Marco
      here is the answer for your question:
      1. Yes it works with HTTPS, but you need to include that option in Curl. Curl support secure connection
      2. WordPress will keep this feature, as it's ability to work with third party application. Ping back using WordPress XMLRPC feature as well.
      Wordpress is powerful CMS, but it can kill your server if it is not carefully configured and optimized.

  3. Friendly Froggy says:

    "How to submit forms using cURL over SSL" would have been better… 🙁

  4. Tyler says:

    I'm new to this, and I keep getting this error:

    Fatal error: Call to undefined function xmlrpc_encode_request()

    I've enabled xmlrpc in the admin panel. I'm on my own server so I may be missing some essential modules or settings. Thank you.

  5. Stian says:

    Hey

    I am using this script and it works perfectly. However, it sometimes returns false even tho the post is added to the blog.

    Do you know why this happens?

    – Stian

  6. Good one, makes to understand the remote publishing to WP easy

  7. Phousley says:

    I'm not getting a response from the CURL so $results is empty… I wanted to verify that a post was made and get the post # – any ideas?

  8. Nandi says:

    Bos Ivan terimakasih banyak code nya, saya masih belum berhasil kalo kita mau multiple category, ada petunjuk?

  9. Anonymous says:

    Hi, i want to insert multiple categories from outsite the function but it doesnt work.
    $category = "'Musicians','Comedians'";

    If i add only one category its working
    $category = "Musicians";

    But multiple i don't know how to write this line.

  10. Ashish says:
  11. Great article Ashish, thanks.

  12. Rehmanafridi2003 says:

    the above code is not executed correctly please make sure

  13. Hi,
    just for completeness: due mainly to personal preference, eventually I figured out another way to post to WordPress from the command line. Here it is:
    http://freesoftware.zona-m.net/how-to-post-content-to-a-wordpress-blog-from-the-command-line/
    HTH,
    Marco

  14. Thanks Marco.

  15. natalstone says:

    Hi Ivan,

    I'm using XML-RPC posting, but in the end my posts appear with broken html tags and it's painful because my links end broken. The problem appears to be in my hosting company, but they can't resolve my problem.

    What can I do solve this issue?

    Thanks!

  16. kartheek periyasami says:

    hi,i am trying to post the values such as title,description and category to a web site using xml-rpc.But it post two values title and description from xml file …..can you tell me coding post with category values????//please

  17. kartheek periyasami says:

    hi,i am trying to post the values such as title,description and category to a web site using xml-rpc.But it post two values title and description from xml file …..can you tell me coding post with category values????//please

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.