Tonight I’ve open sourced the current state of my WordPress XML-RPC plugin containing a slew of new methods. I’m going through the motions to get it on the WordPress.org plugin repository and will update this post with a link when it’s up there.
What is it?
This plugin is primarily a refinement of the work done by Prasath during the WordPress Web Services GSoC 2011 project, as discussed in my last post. This involved a lot of reformatting the code to be self-consistent and closer to the WordPress core code style, as well as simplifying or reorganizing some of the logic.
In an effort to reduce code duplication, I’ve taken a cue from the bbPress XML-RPC implementation and extracted common return-value preparation code into prepare_<datatype> methods. Where sensible, I’ve also removed some input validation that already occurs in the core APIs that are subsequently called.
I had to remove three methods from the original wp-webservices plugin: wp.newPost, wp.editPost, and wp.deletePost. These methods are pretty crucial, but I was unsatisfied with the approach taken in the GSoC project and want to spend more time devising a more sensible implementation before including them in the plugin.
Highlight: wp.getPosts
One of the biggest pain points for the WordPress mobile apps and other XML-RPC integrations is the inability to retrieve anything beyond the latest 10 posts. The metaWeblog.getRecentPosts method lets you specify the number of posts to return, but does not support paging or any sort of filtering, which means that many shared hosts fall over (or time out) if you try to retrieve all the posts at once.
The new wp.getPosts method has two parameters that allow for much greater control over the return values: filter and fields.
The “filter” (associative) array allows you to more precisely control what posts will be returned. It can accept the following keys:
- post_type: the type of post to return (post, page, or any custom post type)
- post_status: only return posts with specific status(es)
- numberposts: the number of posts to return
- offset: combined with numberposts, allows paged return of results
- orderby and order: if you’d prefer an ordering other than reverse-chronological, you can use these parameters to specify the ordering (see wp_get_recent_posts docs for valid values)
The “fields” array allows you to specify the extra set of fields to return in the response for each post. In many scenarios, the calling application does not require all of the fields, so asking for a subset saves on bandwidth and potentially avoids some additional queries on the server.
“fields” can contain individual field names, but can also contain “conceptual group” names, which are sets of associated fields. The available conceptual groups for posts in v0.5 are “post” (most of the basic fields), “taxonomies” (akin to calling wp.getPostTerms), “custom_fields”, and “enclosure”.
Other Features
See the readme.txt file for a full list of methods, including methods for user management, custom taxonomies, and basic information on custom post types. I’ll cover many of these methods in future blog posts.
Next Steps
This release is just the first step in a process of trying to get these methods into core. Tomorrow I’ll be updating patches on all of the relevant tickets and soliciting feedback on certain design and implementation questions. If you have an interest in this, please watch the core Trac (see my last post for specific tickets).
A new branch of python-wordpress-xmlrpc is also forthcoming that includes support for the new methods.