#JSON API A JSON API for WordPress
##Description JSON API allows you to retrieve and manipulate WordPress content using HTTP requests. There are three main goals:
- Provide a simple, consistent external interface
- Create a stable, understandable internal implementation
- Enable new types of extensions for WordPress
This plugin was created at The Museum of Modern Art for the weblog Inside/Out, which is served from Ruby on Rails. Instead of reimplementing the site templates as a WordPress theme, we opted for a Rails front-end that displays content served from a WordPress back-end. JSON API provides the necessary interface for retrieving content and accepting comment submissions.
##Installation
- Upload the
json-apifolder to the/wp-content/plugins/directory or install directly through the plugin installer. - Activate the plugin through the 'Plugins' menu in WordPress or by using the link provided by the plugin installer.
##Table of Contents
##General Concepts
###Requests
Requests use a simple HTTP GET or POST. To invoke the API, include a non-empty query value for json in the URL.
JSON API operates in two modes:
- Implicit mode is triggered by setting the
jsonquery var to a non-empty value on any WordPress page. The content that would normally appear on that page is returned in JSON format. - Explicit mode is triggered by setting
jsonto a known method string. See Request methods for a complete method listing.
#####Implicit mode examples:
http://www.example.org/?json=1http://www.example.org/?p=47&json=1http://www.example.org/tag/banana/?json=1
#####Explicit mode examples:
http://www.example.org/?json=get_recent_postshttp://www.example.org/?json=get_post&post_id=47http://www.example.org/?json=get_tag_posts&tag_slug=banana
#####With user-friendly permalinks configured:
http://www.example.org/api/get_recent_posts/http://www.example.org/api/get_post/?post_id=47http://www.example.org/api/get_tag_posts/?tag_slug=banana
Further reading See Request arguments for more information about request arguments to modify the response.
###Controllers The 1.0 release of JSON API introduced a modular controller system. This allows developers to flexibly add features to the API and give users more control over which methods they have enabled.
#####The Core controller
Most of the methods available prior to version 1.0 have been moved to the Core controller. The two exceptions are submit_comment and create_post which are now available from the Respond and Posts controllers, respectively. The Core controller is the only one enabled by default. All other functionality must be enabled from the JSON API Settings page (under Settings in the WordPress admin menu).
#####Specifying a controller There are a few ways of specifying a controller, depending on how you are calling the API:
http://www.example.org/?json=get_recent_posts(corecontroller is implied, method isget_recent_posts)http://www.example.org/api/info/(corecontroller is implied)http://www.example.org/api/core/get_category_posts/(corecontroller can also be explicitly specified)http://www.example.org/?json=respond.submit_comment(respondcontroller,submit_commentmethod)
Legacy compatibility
JSON API retains support for its pre-1.0 methods. For example, if you invoke the method create_post without a controller specified, the Posts controller is chosen instead of Core.
#####Available controllers The current release includes three controllers:
- Core
- Posts
- Respond
Developers are encouraged to suggest or submit additional controllers.
Further reading See Request methods for a complete reference of available controllers and methods. For documentation on extending JSON API with new controllers see Developing JSON API controllers.
###Responses The standard response format for JSON API is (as you may have guessed) JSON.
Here is an example response from http://localhost/wordpress/?json=1 called on a default WordPress installation (formatted for readability):
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{
"id": 1,
"type": "post",
"slug": "hello-world",
"url": "http:\/\/localhost\/wordpress\/?p=1",
"title": "Hello world!",
"title_plain": "Hello world!",
"content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
"excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
"date": "2009-11-11 12:50:19",
"modified": "2009-11-11 12:50:19",
"categories": [],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "",
"url": "",
"description": ""
},
"comments": [
{
"id": 1,
"name": "Mr WordPress",
"url": "http:\/\/wordpress.org\/",
"date": "2009-11-11 12:50:19",
"content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.<\/p>\n",
"parent": 0
}
],
"comment_count": 1,
"comment_status": "open"
}
]
}##Request methods Request methods are available from the following controllers:
- Core controller - basic introspection methods
- Posts controller - data manipulation methods for posts
- Respond controller - comment/trackback submission methods
- Widgets controller - retrieve sidebar widgets
###Core controller methods The Core controller offers a mostly-complete set of introspection methods for retrieving content from WordPress.
####Method: info
Returns information about JSON API.
#####Optional arguments
controller- returns detailed information about a specific controller
#####Response
{
"status": "ok",
"json_api_version": "1.0",
"controllers": [
"core"
]
}#####Response with “controller=core”
{
"status": "ok",
"name": "Core",
"description": "Basic introspection methods",
"methods": [
...
]
}####Method: get_recent_posts
Returns an array of recent posts. You can invoke this from the WordPress home page either by setting json to a non-empty value (i.e., json=1) or from any page by setting json=get_recent_posts.
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_posts
Returns posts according to WordPress's WP_Query parameters. The one default parameter is ignore_sticky_posts=1 (this can be overridden).
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
Further reading
See the WP_Query documentation for a full list of supported parameters. The post_status parameter is currently ignored.
#####Response
{
"status": "ok",
"count": 1,
"posts": [
{ ... }
]
}####Method: get_post
Returns a single post object.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a post URL idorpost_id- set to the post's IDslugorpost_slug- set to the post's URL slug
#####Optional arguments
post_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"post": { ... }
}####Method: get_page
Returns a single page object.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a page URL idorpage_id- set to the page's IDslugorpage_slug- set to the page's URL slug
#####Optional arguments
children- set to a non-empty value to include a recursive hierarchy of child pagespost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"page": { ... }
}####Method: get_date_posts
Returns an array of posts/pages in a specific date archive (by day, month, or year).
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a date archive page date- set to a date in the formatYYYYorYYYY-MMorYYYY-MM-DD(non-numeric characters are stripped from the var, soYYYYMMDDorYYYY/MM/DDare also valid)
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_category_posts
Returns an array of posts/pages in a specific category.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a category archive page idorcategory_id- set to the category's IDslugorcategory_slug- set to the category's URL slug
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"category": { ... }
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_tag_posts
Returns an array of posts/pages with a specific tag.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a tag archive page idortag_id- set to the tag's IDslugortag_slug- set to the tag's URL slug
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"tag": { ... }
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_author_posts
Returns an array of posts/pages written by a specific author.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on an author archive page idorauthor_id- set to the author's IDslugorauthor_slug- set to the author's URL slug
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"author": { ... }
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_search_results
Returns an array of posts/pages in response to a search query.
#####One of the following is required
- Invoking the JSON API implicitly (i.e.,
?json=1) on a search results page search- set to the desired search query
#####Optional arguments
count- determines how many posts per page are returned (default value is 10)page- return a specific page number from the resultspost_type- used to retrieve custom post types
#####Response
{
"status": "ok",
"count": 10,
"count_total": 79,
"pages": 7,
"posts": [
{ ... },
{ ... },
...
]
}####Method: get_date_index
Returns both an array of date page permalinks and a tree structure representation of the archive.
#####Response
{
"status": "ok",
"permalinks": [
"...",
"...",
"..."
],
"tree": {
"2009": {
"09": 17,
"10": 20,
"11": 7
}
}
}Note: the tree is arranged by response.tree.[year].[month].[number of posts].
####Method: get_category_index
Returns an array of active categories.
#####Optional argument
parent- returns categories that are direct children of the parent ID
#####Response
{
"status": "ok",
"count": 3,
"categories": [
{ ... },
{ ... },
{ ... }
]
}####Method: get_tag_index
Returns an array of active tags.
#####Response
{
"status": "ok",
"count": 3,
"tags": [
{ ... },
{ ... },
{ ... }
]
}####Method: get_author_index
Returns an array of active blog authors.
#####Response
{
"status": "ok",
"count": 3,
"authors": [
{ ... },
{ ... },
{ ... }
]
}####Method: get_page_index
Returns a hierarchical tree of page posts.
#####Response
{
"status": "ok",
"pages": [
{ ... },
{ ... },
{ ... }
]
}####Method: get_nonce
Returns a WordPress nonce value, required to call some data manipulation methods.
#####Required arguments
controller- the JSON API controller for the method you will use the nonce formethod- the method you wish to call (currentlycreate_postis the only method that requires a nonce)
#####Response
{
"status": "ok",
"controller": "posts",
"method": "create_post",
"nonce": "cefe01efd4"
}Further reading To learn more about how nonces are used in WordPress, see Mark Jaquith's article on the subject.
###Pages controller methods
####Method: create_post
Creates a new post.
#####Required argument
nonce- available from theget_noncemethod (call with varscontroller=postsandmethod=create_post)
#####Optional arguments
status- sets the post status ("draft" or "publish"), default is "draft"title- the post titlecontent- the post contentauthor- the post's author (login name), default is the current logged in usercategories- a comma-separated list of categories (URL slugs)tags- a comma-separated list of tags (URL slugs)
Note: including a file upload field called attachment will cause an attachment to be stored with your new post.
####Method: update_post
Updates a post.
#####Required argument
nonce- available from theget_noncemethod (call with varscontroller=postsandmethod=update_post)
#####One of the following is required
idorpost_id- set to the post's IDslugorpost_slug- set to the post's URL slug
#####Optional arguments
status- sets the post status ("draft" or "publish"), default is "draft"title- the post titlecontent- the post contentauthor- the post's author (login name), default is the current logged in usercategories- a comma-separated list of categories (URL slugs)tags- a comma-separated list of tags (URL slugs)
Note: including a file upload field called attachment will cause an attachment to be stored with your post.
####Method: delete_post
Deletes a post.
#####Required argument
nonce- available from theget_noncemethod (call with varscontroller=postsandmethod=delete_post)
#####One of the following is required
idorpost_id- set to the post's IDslugorpost_slug- set to the post's URL slug
###Respond controller methods
####Method: submit_comment
Submits a comment to a WordPress post.
#####Required arguments
post_id- which post to comment onname- the commenter's nameemail- the commenter's email addresscontent- the comment content
#####Optional arguments
redirect- redirect instead of returning a JSON objectredirect_ok- redirect to a specific URL when the status value isokredirect_error- redirect to a specific URL when the status value iserrorredirect_pending- redirect to a specific URL when the status value ispending
#####Custom status values
pending- assigned if the comment submission is pending moderation
###Widgets controller methods
####Method: get_sidebar
Retrieves widgets assigned to a sidebar.
#####Required arguments
sidebar_id- the name or number of the sidebar to retrieve
##Request arguments API requests can be controlled by specifying one of the following arguments as URL query vars.
#####Examples
- Debug the response:
http://www.example.org/api/get_page_index/?dev=1 - Widget-style JSONP output:
http://www.example.org/api/get_recent_posts/?callback=show_posts_widget&read_more=More&count=3 - Redirect on error:
http://www.example.org/api/posts/create_post/?callback_error=http%3A%2F%2Fwww.example.org%2Fhelp.html
###Output-modifying arguments The following arguments modify how you get results back from the API. The redirect response styles are intended for use with the data manipulation methods.
- Setting
callbackto a JavaScript function name will trigger a JSONP-style callback. - Setting
redirectto a URL will cause the user's browser to redirect to the specified URL with astatusvalue appended to the query vars (see Response objects for an explanation of status values). - Setting
redirect_[status]allows you to control the resulting browser redirection depending on thestatusvalue. - Setting
devto a non-empty value adds whitespace for readability and responds withtext/plain - Errors are suppressed unless
devis set to a non-empty value - Setting
json_encode_optionswill let you specify an integer bitmask to modify the behavior of PHP'sjson_encode(Note: this option is only recognized in PHP version 5.3+) - Setting
json_unescaped_unicodewill replace unicode-escaped characters with their unescaped equivalents (e.g.,\u00e1becomes á) - Omitting all of the above arguments will result in a standard JSON response.
###Content-modifying arguments These arguments are available to modify all introspection methods:
date_format- Changes the format of date values. Uses the same syntax as PHP's date() function. Default value isY-m-d H:i:s.read_more- Changes the 'read more' link text in post content.include- Specifies which post data fields to include. Expects a comma-separated list of post fields. Leaving this empty includes all fields.exclude- Specifies which post data fields to exclude. Expects a comma-separated list of post fields.custom_fields- Includes values from posts' Custom Fields. Expects a comma-separated list of custom field keys.author_meta- Includes additional author metadata. Should be a comma-separated list of metadata fields.count- Controls the number of posts to include (defaults to the number specified by WordPress)order- Controls the order of post results ('DESC' or 'ASC'). Default value is 'DESC'.order_by- Controls which field to order results by. Expects one of the following values:authordate(default value)titlemodifiedmenu_order(only works with Pages)parentIDrandmeta_value(meta_keymust also be set)nonecomment_countmeta_key,meta_value,meta_compare- Retrieve posts (or Pages) based on a custom field key or value.
###Using include/exclude and redirects
About include/exclude arguments
By default you get all values included with each post object. Specify a list of include values will cause the post object to filter out the values absent from the list. Specifying exclude causes post objects to include all values except the fields you list. For example, the query exclude=comments includes everything except the comments.
About the redirect argument
The redirect response style is useful for when you need the user's browser to make a request directly rather than making proxy requests using a tool like cURL. Setting a redirect argument causes the user's browser to redirect back to the specified URL instead of returning a JSON object. The resulting status value is included as an extra query variable.
For example calling an API method with redirect set to http://www.example.com/foo will result in a redirection to one of the following:
http://www.example.com/foo?status=okhttp://www.example.com/foo?status=error
You can also set separate URLs to handle status values differently. You could set redirect_ok to http://www.example.com/handle_ok and redirect_error to http://www.example.com/handle_error in order to have more fine-tuned control over the method result.
##Response objects This section describes data objects you can retrieve from WordPress and the optional URL redirects.
Status values
All JSON API requests result in a status value. The two basic status values are ok and error. Additional status values are available for certain methods (such as pending in the case of the submit_comment method). API methods that result in custom status values include a custom status values section in their documentation.
Naming compatibility Developers familiar with WordPress may notice that many names for properties and arguments have been changed. This was a stylistic choice that intends to provide more clarity and consistency in the interface.
###Post response object
id- Integertype- String (e.g.,postorpage)slug- Stringurl- Stringtitle- Stringtitle_plain- Stringcontent- String (modified by theread_moreargument)excerpt- Stringdate- String (modified by thedate_formatargument)modified- String (modified by thedate_formatargument)categories- Array of category objectstags- Array of tag objectsauthorAuthor objectcomments- Array of comment objectsattachments- Array of attachment objectscomment_count- Integercomment_status- String ("open"or"closed")thumbnail- String (only included if a post thumbnail has been specified)custom_fields- Object (included by setting thecustom_fieldsargument to a comma-separated list of custom field names)taxonomy_(taxonomy)- Array of custom taxonomy objects (these resemble Category or Tag response objects, depending on whether the taxonomy is hierarchical)
Note
The thumbnail attribute returns a URL to the image size specified by the optional thumbnail_size request argument. By default this will use the thumbnail or post-thumbnail sizes, depending on your version of WordPress. See Mark Jaquith's post on the topic for more information.
###Category response object
id- Integerslug- Stringtitle- Stringdescription- Stringparent- Integerpost_count- Integer
###Tag response object
id- Integerslug- Stringtitle- Stringdescription- Stringpost_count- Integer
###Author response object
id- Integerslug- Stringname- Stringfirst_name- Stringlast_name- Stringnickname- Stringurl- Stringdescription- String
Note: You can include additional values by setting the author_meta argument to a comma-separated list of metadata fields.
###Comment response object
id- Integername- Stringurl- Stringdate- Stringcontent- Stringparent- Integerauthor- Object (only set if the comment author was registered & logged in)
###Attachment response object
id- Integerurl- Stringslug- Stringtitle- Stringdescription- Stringcaption- Stringparent- Integermime_type- Stringimages- Object with values includingthumbnail,medium,large,full, each of which are objects with valuesurl,widthandheight(only set if the attachment is an image)
##Extending JSON API JSON API exposes several WordPress action and filter hooks as well as a modular controller system for adding new API methods.
###Plugin hooks JSON API exposes several action and filter hooks to augment its behavior.
####Filter: json_api_controllers
This filter controls the array of controllers available to JSON API. The callback function is passed a single argument, an array of strings.
#####Example
<?php
function add_my_controller($controllers) {
// Corresponds to the class JSON_API_MyController_Controller
$controllers[] = 'MyController';
return $controllers;
}
// Add a custom controller
add_filter('json_api_controllers', 'add_my_controller');
?>####Filter: json_api_[controller]_controller_path
Specifies the PHP source file for a given controller, overriding the default location wp-content/plugins/json_api/controllers.
Note
If you your controller file in the json-api/controllers folder JSON API will find it automatically.
#####Example
<?php
function widgets_controller_path($default_path) {
return '/path/to/widgets.php';
}
// Register the source file for JSON_API_Widgets_Controller
add_filter('json_api_widgets_controller_path', 'widgets_controller_path');
?>Capitalization
Your filter hook must be all-lowercase to work correctly. The above example would fail with the filter json_api_Widgets_Controller_path, even if that's how the class is capitalized in the PHP source.
####Filter: json_api_encode
This is called just before the output is encoded into JSON format. The value passed will always be an associative array, according to the format described in each method's documentation. Those items described in Response objects are passed as PHP objects, not associative arrays.
#####Example
<?php
function my_encode_kittens($response) {
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
my_add_kittens($post); // Add kittens to each post
}
} else if (isset($response['post'])) {
my_add_kittens($response['post']); // Add a kittens property
}
return $response;
}
function my_add_kittens(&$post) {
$post->kittens = 'Kittens!';
}
add_filter('json_api_encode', 'my_encode_kittens');
?>####Action: json_api-[controller]-[method]
Each JSON API method invokes an action when called.
#####Example
<?php
// Disable get_author_index method (e.g., for security reasons)
function my_disable_author_index() {
// Stop execution
exit;
}
add_action('json_api-core-get_author_index', 'my_disable_author_index');
?>###Developing JSON API controllers
#####Creating a controller
To start a new JSON API controller, create a file called hello.php inside wp-content/plugins/json-api/controllers. Add the following class definition:
<?php
class JSON_API_Hello_Controller {
public function hello_world() {
return array(
"message" => "Hello, world"
);
}
}
?>Your controller is now available as hello, and exposes one hello_world method.
Next, activate your controller from the WordPress admin interface, available from the menu under Settings > JSON API. You can either click on the link to your hello_world method from the admin interface or enter it manually. It should have the form: http://www.example.org/api/hello/hello_world/?dev=1 or http://www.example.org/?json=hello.hello_world&dev=1 (note the use of the dev argument to enable human-readable output). You should get the following output:
{
"status": "ok",
"message": "Hello, world"
}#####Using query vars
To customize the behavior of your controller, you will want to make use of the global $json_api->query object. Add the following method to your controller:
<?php
public function hello_person() {
global $json_api;
$name = $json_api->query->name;
return array(
"message" => "Hello, $name."
);
}
?>Now append the name query var to the method call: http://www.example.org/api/hello/hello_world/?dev=1&name=Alice or http://www.example.org/?json=hello.hello_world&dev=1&name=Alice.
{
"status": "ok",
"message": "Hello, Alice"
}#####Introspector and data models
Your controller can use any of the existing WordPress functions to collect data, but JSON API also includes an introspector that wraps data in objects defined in the json-api/models directory. These are the same data models described in Response objects.
Here is an example of how you might use the introspector:
<?php
// Retrieve posts based on custom field key/value pair
public function get_custom_posts() {
global $json_api;
// Make sure we have key/value query vars
if (!$json_api->query->key || !$json_api->query->value) {
$json_api->error("Include a 'key' and 'value' query var.");
}
// See also: http://codex.wordpress.org/Template_Tags/query_posts
$posts = $json_api->introspector->get_posts(array(
'meta_key' => $json_api->query->key,
'meta_value' => $json_api->query->value
));
return array(
'key' => $key,
'value' => $value,
'posts' => $posts
);
}
?>#####External controllers
It is recommended that custom controllers are kept outside of json-api/controllers in order to avoid accidental deletion during upgrades or site migrations. To make your controller visible from an external plugin or theme directory you will need to use two filters: json_api_controllers and json_api_[controller]_controller_path. Move the hello.php file from the steps above into your theme's directory. Then add the following to your theme's functions.php file (if your theme doesn't have a file called functions.php you can create one).
<?php
function add_hello_controller($controllers) {
$controllers[] = 'hello';
return $controllers;
}
add_filter('json_api_controllers', 'add_hello_controller');
function set_hello_controller_path() {
return "/path/to/theme/hello.php";
}
add_filter('json_api_hello_controller_path', 'set_hello_controller_path');
?>###Configuration options
The following are constants you can define in your wp-config.php folder:
JSON_API_DIR- set to the directory where JSON API plugin lives (in some cases this can be useful formu-pluginswith WordPress MU)JSON_API_CONTROLLERS- a comma-separated list of default controllers to enable (this is overridden by the JSON API settings page)
##Unit tests JSON API comes with a set of tests that should make it easier to maintain and reveal incompatibilities when they might occur. This is an ongoing process, I hope to improve the test coverage going forward.
###Preparing a WordPress test site There are a few necessary steps that need to be carried out before the test suite will run properly.
- WordPress should generate a new set of tables before you start, so if you're testing with a
wp_table prefix make sure the database has no existing tables of this kind - Configure and install a new copy of WordPress
- Delete the Hello World post and Sample Page (titled "About" in some versions of WordPress)
- Enable user-friendly URLs from Settings > Permalinks, use the "Day and name" format
- Install + Activate the JSON API plugin and enable all bundled controllers from Settings > JSON API
- Import the Theme Unit Test test data XML file from Settings > Import > WordPress (you will need to install the WordPress Importer plugin)
###Running the tests From the command line, make sure you have the HTTP_Client PEAR package installed:
pear install HTTP_Client
Change directory to tests and run the following:
pear run-tests
You should see the test results print out culminating in a summary:
TOTAL TIME: 00:04
23 PASSED TESTS
0 SKIPPED TESTS