WordPress XMLRPC: wp.getPageTemplates

WordPress XMLRPC provides wp.getPageTemplates to

Retrieve page templates.

Parameters

  • int blog_id
  • string username
  • string password

Return Values

  • array
    • struct
      • string name
      • string description

Let’s see the program. Note that you must fill the following parameters

  1. $USERNAME
  2. $PASSWORD
  3. $BLOGID
  4. $BLOGURL

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID.

<?php
 
$USERNAME = "admin";
$PASSWORD = "check";
$BLOGID = 1;
$BLOGURL = "http://localhost/wordpress";

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

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 wp.getPageTemplates request */
  $request = xmlrpc_encode_request("wp.getPageTemplates",
    array($BLOGID, $USERNAME, $PASSWORD));
 
  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);
 
  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>

The output will be something like
[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>Links</name>
<value>
<string>links.php</string>
</value>
</member>
<member>
<name>Archives</name>
<value>
<string>archives.php</string>
</value>
</member>
<member>
<name>Default</name>
<value>
<string>default</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
[/xml]

Advertisements

WordPress XMLRPC: wp.getPageStatusList

WordPress XMLRPC provides wp.getPageStatusList to retrieve all of the WordPress supported page statuses.

Parameters

  1. int blog_id
  2. string username
  3. string password

Return Values

  • struct
    1. const string ‘draft’
    2. const string ‘private’
    3. const string ‘publish’

Let’s see the program. Note that you must fill the following parameters

  1. $USERNAME
  2. $PASSWORD
  3. $BLOGID
  4. $BLOGURL

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID.

<?php
 
$USERNAME = "admin";
$PASSWORD = "check";
$BLOGID = 1;
$BLOGURL = "http://localhost/wordpress";

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

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 wp.getPageStatusList request */
  $request = xmlrpc_encode_request("wp.getPageStatusList",
    array($BLOGID, $USERNAME, $PASSWORD));
 
  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);
 
  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>

Let’s execute the program

$ php wp.getPageStatusList.php

The output will be something like
[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>draft</name>
<value>
<string>Draft</string>
</value>
</member>
<member>
<name>private</name>
<value>
<string>Private</string>
</value>
</member>
<member>
<name>publish</name>
<value>
<string>Published</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
[/xml]

WordPress XMLRPC: wp.getPostStatusList

WordPress XMLRPC provides  wp.getPostStatusList to retrieve post statuses.

Parameters

  1. int blog_id
  2. string username
  3. string password

Return Values

  • array
    1. const string ‘draft’
    2. const string ‘pending’
    3. const string ‘private’
    4. const string ‘publish’

Let’s see the program. Note that you must fill the following parameters

  1. $USERNAME
  2. $PASSWORD
  3. $BLOGID
  4. $BLOGURL

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID.

<?php

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

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

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 wp.getPostStatusList request */
  $request = xmlrpc_encode_request("wp.getPostStatusList",
    array($BLOGID, $USERNAME, $PASSWORD));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>

Let’s execute the program

$ php wp.getPostStatusList.php

The output will be something like

[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>draft</name>
<value>
<string>Draft</string>
</value>
</member>
<member>
<name>pending</name>
<value>
<string>Pending Review</string>
</value>
</member>
<member>
<name>private</name>
<value>
<string>Private</string>
</value>
</member>
<member>
<name>publish</name>
<value>
<string>Published</string>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>

[/xml]

WordPress XMLRPC: wp.getCommentCount

WordPress XMLRPC gives the following function wp.getCommentcount to retrieve comment count for a specific post.

Parameters for the function

  1. int blog_id
  2. string username
  3. string password
  4. string post_id

Return Values

  • array
    • struct
      1. int approved
      2. int awaiting_moderation
      3. int spam
      4. int total_comments

The return value gives the details of the comment like the number of comments approved for a particular posts, comments to be moderated, comments marked as spam

Let’s see the program. Note that you must fill the following parameters

  1. $USERNAME = “admin”;
  2. $PASSWORD = “check”;
  3. $BLOGID = 1;
  4. $BLOGURL = “http://localhost/wordpress&#8221;;
  5. $POSTID = 20;

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID. POSTID is the ID of the post for which you want to find the comment count.

<?php

$USERNAME = "admin";
$PASSWORD = "check";
$BLOGID = 1;
$BLOGURL = "http://localhost/wordpress";
$POSTID = 20;

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

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 wp.getCommentCount request*/
  $request = xmlrpc_encode_request("wp.getCommentCount",
    array($BLOGID, $USERNAME, $PASSWORD, $POSTID));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>

Let’s execute the program

$ php  wp.getCommentCount.php

The output will be something like this

[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>approved</name>
<value>
<int>0</int>
</value>
</member>
<member>
<name>awaiting_moderation</name>
<value>
<int>0</int>
</value>
</member>
<member>
<name>spam</name>
<value>
<int>0</int>
</value>
</member>
<member>
<name>total_comments</name>
<value>
<int>0</int>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>
[/xml]

WordPress XMLRPC: wp.getTags

WordPress XMLPRC provides the function wp.getTags to get all the tags corresponding to a blog.

Parameters to this function:

  1. int blog_id
  2. string username
  3. string password

Return Values

  • array
    • struct
      1. int tag_id
      2. string name
      3. int count
      4. string slug
      5. string html_url
      6. string rss_url

It returns an array of tags. Each array element contains the details of the tag like the HTML URL, RSS URL, tag count.

The following program shows the usage of wp.getTags. Note that you must fill the following values

  1. $USERNAME
  2. $PASSWORD
  3. $BLOGID
  4. $BLOGURL

Note that BLOGID is usually one or you can use wp.getUsersBlogs to get the value of BLOGID.

<?php

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

$XMLRPC_URL = $BLOGURL."/xmlrpc.php";

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 wp.getTags request which takes on two parameters
   username and password*/
  $request = xmlrpc_encode_request("wp.getTags",
    array($BLOGID, $USERNAME, $PASSWORD));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($XMLRPC_URL, $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "n";
?>
}

Let’s execute the program

$ php wp.getTags.php

The output will be something like

[xml]
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>7</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>install libboost-python-dev ubuntu</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>1</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>install-libboost-python-dev-ubuntu</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=install-libboost-python-dev-ubuntu</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=install-libboost-python-dev-ubuntu</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>9</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>install lobasis3.3-javafilter ubuntu</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>install-lobasis3-3-javafilter-ubuntu</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=install-lobasis3-3-javafilter-ubuntu</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=install-lobasis3-3-javafilter-ubuntu</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>10</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>java</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>java</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=java</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=java</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>6</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>libboost-python-dev</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>1</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>libboost-python-dev</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=libboost-python-dev</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=libboost-python-dev</string&gt;
</value>
</member>
</struct>
</value>
<value><struct>
<member>
<name>tag_id</name>
<value>
<string>8</string>
</value>
</member>
<member>
<name>name</name>
<value>
<string>lobasis3.3-javafilter</string>
</value>
</member>
<member>
<name>count</name>
<value>
<string>2</string>
</value>
</member>
<member>
<name>slug</name>
<value>
<string>lobasis3-3-javafilter</string>
</value>
</member>
<member>
<name>html_url</name>
<value>
<string>http://localhost/wordpress/?tag=lobasis3-3-javafilter</string&gt;
</value>
</member>
<member>
<name>rss_url</name>
<value>
<string>http://localhost/wordpress/?feed=rss2&amp;amp;tag=lobasis3-3-javafilter</string&gt;
</value>
</member>
</struct>
</value>
</data>
</array>
</value>
</param>
</params>
</methodResponse>

[/xml]

XMLRPC: demo.addTwoNumbers

With demo.sayHello, you saw how to make use of wordpress XMLRPC functions. But demo.sayHello doesn’t take any parameters as arguments. To show how to pass arguments, let’s explore demo.addTwoNumbers.

It takes two integers as arguments and returns a response which is the sum of the two numbers

<?php

/*The URL of the blog*/
$BLOGURL = "http://localhost/wordpress";

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 demo.addTwoNumbers request which takes on two parameters
 num1: number 1 
 num2: number 2
 returns the sum
 */
function demo_addTwoNumbers($num1, $num2) {
   global $BLOGURL;
   $request = xmlrpc_encode_request("demo.addTwoNumbers",
      array($num1, $num2));
  
    /*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*/
    print_r($response);
}

demo_addTwoNumbers(1, 2);
echo "n";
?>

As you see, that we pass two numbers.

On executing the program
[xml]
$ php demo.addTwoNumbers.php

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<int>3</int>
</value>
</param>
</params>
</methodResponse>
[/xml]

XMLRPC- Hello World- demo.sayHello

With WordPress XMLRPC, you can blog from your desktop client. WordPress XMLRPC provides a series of methods to perform various operations which you normally do from your WordPress website. To start working with wordpress XMLRPC, let’s take a  simple “Hello World!” program

<?php

/*The URL of the blog*/
$BLOGURL = "http://localhost/wordpress";

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 demo.sayHello request
 */
function demo_sayHello() {
   global $BLOGURL;
   $request = xmlrpc_encode_request("demo.sayHello", "");

    /*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*/
    print_r($response);
}

demo_sayHello();
echo "n";
?>

On executing this simple program, you will get a simple “Hello World!” message as the response. This is a simple method which doesn’t take any arguments. All you have to do is to make a call to “demo.sayHello”

[xml]
$ php demo.sayHello.php

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<string>Hello!</string>
</value>
</param>
</params>
</methodResponse>

[/xml]