WordPress as XML-RPC client?

I want to sync post edits on two sites. Test environment, not production. Single direction (site A to site B, but not backwards).

Basically I edit post at my local test stack and same post (part of test content set) at remote server should be updated with identical copy of resulting content.

I know that XML-RPC server is implemented in WordPress… But WordPress itself is not a XML-RPC client and I have no idea how easy would it be to make it into one (I am usually wary of XML-anything 🙂

So should I go with XML-RPC and implement client functionality or that is not worth the time and I should build custom form or something at remote server to catch changes?

1

WordPress already has a XML-RPC client class implemented. It’s in the same file as the server part: class-IXR.php located in wp-includes.

The following code will generate a new post. You could wrap this in a function and attach it to the save_post/update_post action hook. To sync both parts, you could check for the post-slug or submit the same post-id to the post in the second blog.

$rpc = new IXR_Client('http://second-blog-domain.tld/path/to/wp/xmlrpc.php');

$post = array(
    'title' => 'Post Title',
    'categories' => array('Category A', 'Category B'),
    'mt_keywords' => 'tagA, tagB, tagC',
    'description' => 'Post Content',
    'wp_slug' => 'post-slug'
);

$params = array(
    0,
    'username',
    'password',
    $post,
    'publish'
);

$status = $rpc->query(
    'metaWeblog.newPost',
    $params
);

if(!$status) {
    echo 'Error [' . $rpc->getErrorCode() . ']: ' . $rpc->getErrorMessage();
    exit();
}

Leave a Comment