XML-RPC in WordPress

XML-RPC is a protocol for remote procedure calls which uses XML for the data exchange and it mostly uses HTTP for the actual call. In XML-RPC the client that wants to make a call to a remote method creates the input parameters in the form of XML and sends it via an HTTP request to a remote server implementing the XML-RPC protocol. The remote server implementing the XML-RPC protocol gets the request and then performs the remote method and returns the result back in XML format.

In this article we are going to see how to call methods on your WordPress installation using XML-RPC.

Step 1 Understanding XML-RPC in WordPress

WordPress is a complete blogging platform. It is made highly customizable and open for other systems to connect and communicate with it. XML-RPC in WordPress helps this openness of WordPress by letting other systems or software perform operations on the WordPress installation, even remotely. So using WordPress XML-RPC can create WordPress blogging clients, some other software which does some batch tasks like creating multiple posts from a file, etc.

Step 2 Enabling XML-RPC Support on You WordPress Installation

By default XML-RPC is not enabled on your WordPress installation. So in order for your WordPress installation to be able to serve XML-RPC requests you have to enable this feature on you website.

To enable this feature you will have go to the admin section. There, under Settings -> Writing you will have to Enable the XML-RPC option as shown below.

Step 3 Knowing the Files and Classes in WordPress That Make XML-RPC Possible

All the XML-RPC requests are sent to the xmlrpc.php file which is present in your main WordPress installation directory. The file basically does a bootstrap for loading the WordPress environment and then creates an object of the class wp_xmlrpc_server which is present at the location wp-includes/class-wp-xmlrpc-server.php.

The wp_xmlrpc_server class is responsible for handling all the XML-RPC request coming from the XML-RPC clients.

Then we will create a send_request function which will take the request name and the parameters for the request as input. The function will then create an XML request from the input parameters and then send the request to the WordPress installation XML-RPC URL and return the results.

This function uses the PHP function xmlrpc_encode_request which creates the XML request then we use curl to send the request on the XML-RPC URL of the WordPress installation stored in the class member variable.

Then this function returns the result which it gets from the XML-RPC server.

To check if we are able to call the methods on the WordPress installation server properly we will call demo.sayHello method, it takes no parameter and which if runs correctly the WordPress XML-RPC server returns a string "Hello!".

This basically takes the title and body as inputs and then creates the parameters required for the request, then sends the request.

We will call the function as follows

$objXMLRPClientWordPress->create_page('WordPress XML-RPC page', 'This is the content of First Page done via XML-RPC');

And then call:

$objXMLRPClientWordPress->sayHello()

The return for this request is the page id of the newly created page. Now if we run this function and check our WordPress installation, the newly created page will be seen as follows.

Step 9 Getting the List of Authors of a Blog Using XML-RPC

Now we will create a function to get the author list from your WordPress installation via XML-RPC. The request for getting the list of authors is wp.getAuthors which is also a part of the WordPress API.

I am Abbas Suterwala, a software engineer by profession and a passionate coder. I live every moment at the fullest. I love open source projects and mobile development, and I am especially interested in WordPress as blogging platform. When I am not chilling around with friends, I stay occupied with one of the following open source projects I have done: Choomantar [Steganography ], the Browser Counter WordPress plugin, and Google Buzz From Admin.