How Can I Modify The WordPress XML-RPC System?

|

One of the great things about WordPress plugin development is the abundance of open source content to learn from. Ever see a plugin in action and wonder, “how does that work?” or “How can I modify the WordPress XML-RPC system?” Well, all you have to do is download it and see! Most plugins are licensed under the GPL license (General Public License).

One of my recent projects involves working with the WordPress XML-RPC interface. The problem is I needed the XML-RPC interface to do more than the current implementation offered. Initially I wrote a patch for the xmlrpc.php file to accomplish what I needed. It never actually occurred to me that the smart folks at WordPress would actually give you a way to write hooks into the XML-RPC interface to add more functionality. Sweet! I came to this revelation thanks to a plugin called wpLinkMentor, a plugin that allows you to manage your blog links over XML-RPC.

Logging

An important part of most applications is logging, right? This provides an insight into what is going on with your program/plugin and can be extremely helpful in debugging problems you might encounter. The following snippet allows your plugin to make log entries into the standard WordPress XML-RPC log.

function wp_logIO($io,$msg)
{
  $fp = fopen("../xmlrpc.log","a+");
  $date = gmdate("Y-m-d H:i:s ");
  $iot = ($io == "I") ? " Input: " : " Output: ";
  fwrite($fp, "\n\n".$date.$iot.$msg);
  fclose($fp);
}

Error Handling

The way to handle errors in your XML-RPC plugin is to throw an IXR_Error which is somewhat like an exception in other languages.

return new IXR_Error(403, 'Bad login/pass combination.');

Making The Magic Hook!

Ok, all of that is fine and dandy, but how do I hook into the XML-RPC system? Where’s the beef!? Here it is in all of it’s simplistic glory, just like most of the WordPress architecture.

function attach_new_xmlrpc($methods) {
    $methods['Your XML-RPC Method Name'] = 'Your PHP Method Name';
    $methods['Your XML-RPC Method Name 2'] = 'Your PHP Method Name 2';
    return $methods;
}

add_action('xmlrpc_methods', 'attach_new_xmlrpc');

I could not have imagined it being so simple. Just write your methods to do whatever you desire and them register them as in the previous code sample. Have you written any WordPress plugins that utilize the XML-RPC interface?