Using Wordpress XMLRPC services
Jul 5th, 2009 | By admin | Category: Featured, PHP, TutorialsWordpress is a great blogging platform. With tons of plugins available, Wordpress is quickly become a good choice not only for personal blogging purpose but also for making content oriented sites. There are also many sites want to add ‘blog’ feature into it and WP one more time is the chosen one.
Although WP’s plugin architecture is really not good, the WP’s team have added in a really interesting tool that help integration between WP blog and hosting site easier. It’s XMLRPC services. Here are some scenarios if you has a WP blog and you want to integrate it with another site, you will found XMLRPC helpful:
- Showing latest posts/comments on the site homepage
- Provide links to categories in your blog
- Turning your WP blog into a collaborative environment for your site’s authors
Off course, the 3rd scenario look very interesting especially in case your site is quite static, you need to update content by manually update HTML files or you are using some kinds of simple content management tools that do not support multiple user, authorizing process, tagging or categorizing. Though in the limitation of this post, I can only show how to use WP’s XMLRPC.
So, why XMLRPC why I can write direct SQL queries to get data (post, comment, category, etc.) from WP’s database ?
The answer is simple, do not reinvent the wheels. You can write queries for sure, but how about building friendly urls to the posts if WP change the perma link format ? Are your sure you understand the wp_terms table well enough for getting list of categories ? And are you sure to handle the logic of post status, post types, total posts in a category, etc. well ?
Those are enough to decide that XMLRPC must be use when an external want to get something from your WP’s blog. It’s no need to mention about the situation you run your site on one server and WP on another.
Turn it on !
XMLRPC is turn off by default, otherwise some wise users of your blog who has access to it can take your content away. So, turn it on with care. You know where it’s but you might not know what it’s about. Just in case you don’t remember, go to Options > Writing page and check the option Enable XMLRPC.
Get list of available services
Now you can use XMLRPC, just get list of what services you can use. It’s provided on WP’s codex XML RPC page:
http://codex.wordpress.org/XML-RPC
A simple usage sample
include_once('wp-includes/class-IXR.php');
define('BLOG_ID', 0);
define('RPC_USERNAME', 'admin');
define('RPC_PASSWORD', '***');
$client = new IXR_Client('http://your.blog.path/xmlrpc.php');
$client->query('wp.getCategories', BLOG_ID, RPC_USERNAME, RPC_PASSWORD);
$categories = $client->getResponse();
print_r($categories);
To use XMLRPC services, you must create an IRX_Client object. If you want to go in deep, the implementation code of this class is in wp-includes/class-IXR.php. The server that IRX_Client object will work with is defined by your blog’s url appended by xmlrpc.php. You must initialize the $client with this url.
Now, to call a service, the service name is always the first param of $client->query() following by service’s parameters. Most of the time, you need to provide BLOG_ID, username and password to login.
the getResponse() method of IRX_Client object will return result of your query, normally an array as in this case, we get list of categories in our blog. Simple and easy !
OK, but you don’t see what you need in the service list
Sometimes, you don’t. Though there are many services implemented by the WP’s team, you must know somehow to extend the list. This is simple if you are familiar with some WP functions. Firstly, open the xmlrpc.php file and search for ‘function wp_xmlrpc_server’. This function is a map of XMLRPC query name and the actual functions that implement the logic.
$this->methods = array( // WordPress API 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', 'wp.getPage' => 'this:wp_getPage', 'wp.getPages' => 'this:wp_getPages', 'wp.newPage' => 'this:wp_newPage', 'wp.deletePage' => 'this:wp_deletePage', 'wp.editPage' => 'this:wp_editPage', 'wp.getPageList' => 'this:wp_getPageList', 'wp.getAuthors' => 'this:wp_getAuthors', 'wp.getRecentPosts' => 'this:wp_getRecentPosts', 'wp.getCategories' => 'this:mw_getCategories', // Alias //Still long list of services for other blogging platforms
Just take a look of one service, for example, the wp.getCategories we use and then you know how to write yours. Here is a sample of getRecentPosts that I added in my XMLRPC:
function wp_getRecentPosts($args){
$this->escape($args);
$blog_ID = (int) $args[0];
$username = $args[1];
$password = $args[2];
$limit = $args[3];
if ( !$user = $this->login($username, $password) ) {
return $this->error;
}
$posts = wp_get_recent_posts($limit);
foreach($posts as &$post){
//use guid field as perma link
$plink = get_permalink($post['ID']);
$post['guid'] = $plink;
//Get author
$author = get_userdata($post['post_author']);
$post['post_author'] = $author->user_login;
}
return $posts;
}
That is. I am sure that if you spend time to write all the get_userdata, get_permalink, wp_get_recent_post stuff, you will need day(s). But if you use XMLRPC and just in case you don’t work with WP usual, refer to WP Codex’s functions reference page for utility functions you need, you can get what you need in hours.

(1 votes, average: 4.00 out of 5)
[...] Go here to see the original: Using Wordpress XMLRPC services | PHP made simple [...]