WordPress XMLRPC- metaWeblog.newPost

With wordpress XMLRPC, you can write your own client to blog from your desktop. metaWeblog.newPost takes five parameters

  1. Blog ID
  2. Username
  3. Password
  4. Content of the Post
  5. To Publish or not

The fifth parameter is a boolean value, true or false, whether to publish the post or not

The fourth argument, contents of a blog must have the following attributes

  1. Title
  2. Date in ISO8601 format
  3. Description
  4. Categories

Note that the categories must be an existing one. You cannot create a new category with this function. The return value of this function is the ID of the post.

Let’s see a simple PHP program


$BLOGURL = "http://localhost/wordpress";
$USERNAME = "admin";
$PASSWORD = "check";

function get_response($URL, $context) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installedn");

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;

  /*Creating the metaWeblog.newPost request which takes on five parameters

  /*The title of your post*/
  $title = "Sample Post Title";

  /*The contents of your post*/
  $description = "This is a sample post.";

  /*Forming the content of blog post*/
  $content['title'] = $title;
  $content['description'] = $description;
  $content['categories'] = array("Blogging", "category1");
  /*Whether the post has to be published*/
  $toPublish = true;
  $request = xmlrpc_encode_request("metaWeblog.newPost",
    array(1,$USERNAME, $PASSWORD, $content, $toPublish));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
  $response = xmlrpc_decode($xmlresponse);

  /*Printing the response on to the console*/
  echo "n";

To execute the program

$ php metaWeblog.newPost.php

The output will be something like this. Note the post ID.

<?xml version="1.0"?>

Executing the above program will create multiple posts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s