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.

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.

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.

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?

  • rio

    Yeah,
    nice article! Just as addiction to you article let’s say that I want to add a method to the existing RPC methods in WP. Here is how I did it:
    1. Go and find in “/xmlrpc.php” the following row: “$this->methods = array(” and change it to “$this->methods = array(‘wp.myMethod’ => ‘this:wp_myMethod’,”
    2. Write your own method: “function wp_myMethod() {…”. As you already know from this article, all error may be returned with “return new IXR_Error(500, __(‘My Error message’));” etc.
    3. You may take a look at the “blogger_newPost”, “blogger_editPost” and “blogger_deletePost” to get more familiar at how exactly the methods are working.
    4. nJoy your new wordpress xmprpc method! πŸ˜‰

  • Thanks for the great feedback, Rio. I actually went that route as my initial modification. It ran great.. until I updated WordPress. πŸ™ That’s when I rewrote it to use the hooks instead and subsequently published this article.

    Thanks for stopping by!

  • rio

    πŸ™‚ To save your time you could make the hooks as a plugin and then you won’t have to rewrite the xmlprc engine again and again after each update. All you have to do is to install the plugin you created after an update. πŸ˜‰

  • Justin

    Thanks for the great article, Jonathan. I’m new to both wordpress and php (though not to software development), and would like to write my own XML-RPC function. Please let me know to which file(s) you added the hook and the logging function to make it all work. Also, does this file get overwritten when wordpress is updated? I got the impression from your article that it will not, but would appreciate it if you could elaborate.

    I looked up the add_action api and it was not clear about which files to add the add_action hook:

    http://codex.wordpress.org/Function_Reference/add_action

    Thanks again for your help!

    • Hello, Justin. In response to your comment I have created another post that includes a complete example of a WordPress plugin to add a function to the XML-RPC server. There’s also a Java file for testing the new function. You can find the post here:

      A WordPress Plugin – Hello XML-RPC World

  • Pingback: A WordPress Plugin - Hello XML-RPC World | franzone.com()