2011
04.07

I’ll explain now in a few steps, how to write a simple php script,
that can communicate and exchange data with a WordPress based site, that has XML-RPC support enabled.

First, we need the IXR client library (The Incutio XML-RPC Library for PHP),
which is actually bundeled with every WordPress installation, and located in the wp-includes/class-IXR.php file.

So, we can now refer to this file.

An quick XML-RPC example will be go get a category list, first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://domain.com/xmlrpc.php');
     
        $USER = 'admin';
        $PASS = 'mypassword';
               
        if (!$client->query('wp.getCategories','', $USER,$PASS))
        {  
            echo('Error occured during category request.' . $client->getErrorCode().":".$client->getErrorMessage());  
        }
        $cats = $client->getResponse();
       
        if(!empty($cats))
        {
            echo 'Categories:<br />';
            foreach($cats as $_cat) echo $_cat['categoryName'];
        }
       
?>

That’s pretty simple.

Now, let’s get a post and it’s content’s.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://domain.com/xmlrpc.php');
     
        $USER = 'admin';
        $PASS = 'mypassword';
               
        if (!$client->query('metaWeblog.getPost',$post_id, $USER,$PASS))
        {  
            echo('Error occured during getting post.' . $client->getErrorCode().":".$client->getErrorMessage());  
        }
        $post = $client->getResponse();
       
        var_dump($post);
?>

Now, let’s see how to create a new post,
with custom field, tags, and categories:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://domain.com/xmlrpc.php');
     
        $USER = 'admin';
        $PASS = 'mypassword';

        $content['title'] = 'Test title '.mt_rand();
        $content['categories'] = array("NewCategory","Nothing");
        $content['description'] = '<p>Lorem ipsum dolor sit amet</p>';
        $content['custom_fields'] = array( array('key' => 'my_custom_fied','value'=>'yes') );
        $content['mt_keywords'] = array('foo','bar');
       
        if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true))
        {
            die( 'Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage());  
        }
        $ID =  $client->getResponse();
       
        if($ID)
        {
            echo 'Post published with ID:#'.$ID;
        }
       
?>

The first question that would came to your mind once you’ve really tasted the xmlrpc thing, “How could I extend this?”. Well, there’s a simply way to extend this, like anything around WordPress, it can be done, with hooks.

For example create a my-xmlrpc.php in wp-content/plugins with this content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/*
Plugin Name: My xmlrpc
Plugin URI: http://djzone.im
Description: Custom XMLRPC methods for me ;)
Version: 1.2
Author: DjZoNe
Author URI: http://djzone.im
*/


add_filter('xmlrpc_methods', 'my_xmlrpc_methods');

function my_xmlrpc_methods($methods)
{
        $methods['wp.uploadFile'] = 'my_uploadFile';
        $methods['my.addTaxonomy'] = 'my_addTaxonomy';
        unset($methods['wp.getUsersBlogs']);

        return $methods;
}

function my_uploadFile($args)
{
    //replace the original function
}

function my_addTaxonomy($args)
{
    global $wp_xmlrpc_server;

    $wp_xmlrpc_server->escape($args);
   
    $post_ID     = (int) $args[0];
    $username  = $args[1];
    $password   = $args[2];
    $taxonomy = $args[3];
    $data     = (array)$args[4];
   
    if ( !$user = $wp_xmlrpc_server->login($username, $password) )
        return $wp_xmlrpc_server->error;

    do_action('xmlrpc_call', 'my.addTaxonomy');
   
    do_action('xmlrpc_call', 'my.add'.ucfirst($taxonomy));

    wp_delete_object_term_relationships($post_ID,$taxonomy);
   
    $ret = wp_set_object_terms($post_ID , $data, $taxonomy, true);
   
    return $ret;
}

This example shows, how to add a new method (my.addTaxonomy), how to remove a method (wp.getUsersBlogs), and how to overwrite an existing method (wp.uploadFile).

That’s all for now :)

16 comments so far

Add Your Comment
  1. This recent post can complete the above interesting article about connection with light protected server using http basic authentication:
    http://dev.xiligroup.com/?p=2260

  2. Hey Thanks A lot…..This article really helpfull,now i manage my wordpress site by my core php site :)

  3. Glad to hear that Emma :)

  4. Its really helpful to me. Thanks a lot

  5. hi, thanks for this article but it dont work for me , a have an Error
    “server error. requested method wp.my_uploadFile()” does not exist
    this is my client :

    function newload() {

    var request = new XmlRpcRequest(“http://127.0.0.1:8081/wordpressf/xmlrpc.php”,”wp.my_uploadFile()” );
    var response = request.send();

    var id=response.parseXML();
    }

    plllzzzz help :(

  6. You must be missed something dude :)

    We don’t give the FUNCTION NAME in the XMLRPC call, but the predefined method name in the $methods array,
    which should be ‘wp.uploadFile’.

  7. very helpful for me to write web services on wordpress.
    THANKS!

  8. Can you tell me bro how to just login in admin remotely with xmlrpc?

  9. All XMLRPC commands do the login, that’s why you have to add user and pass to every single command.

  10. nice post thanks
    but I can not make it working using class-IXR.php as a remote client which connects to
    my******domain.com/wp-content/plugins/my-xmlrpc.php

    getting this error:
    An error occurred – -32601 : server error. requested method my.newTest does not exist.

    do i use right url to connect?
    tnx

  11. hi
    sorry forgot to activate plugin:)
    works now

  12. Thank you for your information about XMLRPC WordPress :)

  13. Very useful. Thank you!

  14. Great job! Really made easy, thanks!

  15. Hiii i am new in php and i got this exception when i run my project please help me as soon as possible its very urgent ……

    Thanks

    Warning: require(class-IXR.php) [function.require]: failed to open stream: No such file or directory in C:\AppServ\www\IRX\index.php on line 9

    Fatal error: require() [function.require]: Failed opening required ‘class-IXR.php’ (include_path=’.;C:\php5\pear’) in C:\AppServ\www\IRX\index.php on line 9

  16. Nicely presented information, thank you.