Part 1 in a series on working with the WordPress XML-RPC API with PHP.
Lately I’ve had cause to work with WordPress’s implementation of XML-RPC, which is basically a kind of SOAP service that lets you view, add, edit and remove content from outside of your WordPress install.
XML-RPC has been part of WordPress since its initial public release some 11 years ago, but is usually scorned as little more than an efficient attack vector. Which is a fair assessment; few end users need the ability to remotely publish content.
But over the years — especially the last two — as WordPress has melded into a turnkey content management solution, XML-RPC has been improved, both in terms of its base security and its functionality.
Today, it’s perfectly positioned to be a great way to manage content from outside of WordPress itself; that is, to bring in content from third-party systems (which is how I am doing it) to automating virtually any task you have that involves the actual content of your blog.
There aren’t any current libraries or samples, specific to the WordPress XML-RPC API, that are right handy and which explore the full functionality of the API.
The documentation of the WordPress XML-RPC API is, in a word, Spartan.
And Google isn’t much more help, as most of the discussions you’ll find with a search there are years old, and won’t work with the recent overhaul of XML-RPC in WordPress.
That’s proved to be a major pain for me, so over the next few posts here, I’ll discuss each of the current WordPress XML-RPC API methods, and how to properly script them in PHP.
My intent is to also put this code into Github, so that somewhere, out there, will be a WordPress-specific library, built and maintained by a community and able to be changed as WordPress changes, for working with XML-RPC.
The WordPress XML-RPC API Overview
WordPress allows you to interact with all of its basic content types directly from the XML-RPC API:
- posts, including pages and custom post types;
- taxonomy, including categories, tags and custom taxonomy;
- media, including images, PDFs and other attachments you can upload in the Media Library;
- your blog’s options settings; and
What that means is that you’re pretty much able to do anything you want to do with content, but not much with the actual presentation of your content.
- Need to add an image to your blog? Sure!
- Want to edit a page? Done and done.
- Like to keep your WordPress users synced up with another tool, such as Active Directory or your HR database? Not simple, but doable.
- Hankering to change themes on the fly? Unwise, but possible.
- Decide to install a brand-new template or plugin? No dice.
Not Plug And Play, But Not Difficult
Working with XML-RPC in PHP isn’t overly complicated, but it does require you to have some basic coding skill — especially because how you send information to the WordPress XML-RPC API can affect whether you get back the results you were expecting.
To use the built-in XML-RPC functions in PHP, your server needs to be configured with libxml extension and complied with xmlrpc support. Almost any shared Web hosting company is going to have done that for you.
You’ll also need curl to be able to send your XML-RPC requests out and get back responses from WordPress. Once again, just about every Web host out there is going to have curl installed.
With that built-in support, sending and receiving XML-RPC requests is just a few lines of code: Create an array of key-value pairs for your data, provide necessary header information (such as a user name and password), fire it off to WordPress via curl, and interpret the response.
Here’s a quick look at inserting a story via XML-RPC:
$post = array( 'post_type' => 'post', 'post_status' => 'draft', 'post_title' => 'Test Post', 'post_content' => 'This is my test post', 'post_author' => 1 ); $data = xmlrpc_encode_request('wp.newPost', array(0, 'username', 'password', $post); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.yoursite.com/xmlrpc.php'); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $exec = curl_exec($ch); $response = xmlrpc_decode($exec); curl_close($ch); var_dump($response);
Don’t worry, I’ll expand on this in my next post.
All links in this post on delicious: https://delicious.com/dougvdotcom/using-wordpress-xml-rpc-with-php-introduction