WordPress as XML-RPC client?

The question:

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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 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();
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment