Friday, March 29, 2024

Provide a JSON Feed from your WordPress Site using the JSON API Plugin

So you’ve decided to make your WordPress content available to subscribers via a feed. Congratulations! Now which format would you like to use? RSS? Atom? How about JSON? It’s compact and super-easy to work with. Unfortunately, WordPress doesn’t provide a native mechanism for outputting JSON. But that’s not a deal breaker because there are a number of good plugins that allow you to very easily add the functionality you need to deliver your data as JSON. In today’s article, we’ll be using the JSON API plugin to do just that. It works quite well right out of the box, but also has a very detailed API for fetching the exact data that you’re interested in.

Download and Installation

The JSON API plugin was developed by Dan Phiffer for the Museum of Modern Art’s Inside/Out weblog and released on Github in November of 2009. Although the plugin has not been updated in over two years, it still works with newer versions of WordPress and is widely regarded as the best plugin of its type out there.

Rather than download it directly from Github, I would recommend doing so through WordPress’s Plugins > Add New menu command. On the Add Plugins page, perform a keyword search for “json api”. It should be the first item in the results:

add_plugins_page.jpg

From there, click the Install Now button to let WordPress handle the downloading and installation details.

Once it’s done, don’t forget to activate the plugin!

Calling Plugin Methods

Requests use a simple RESTful style in that the client doesn’t know anything about the structure or inner-workings of the API. Rather, the server provides whatever information the client needs to interact with the service – i.e. the operating manual. At its most basic, all we need to do to use the JSON API is include a non-empty query value for the json query parameter in the URL or via POST request.

This can be accomplished in one of two modes: Implicit or Explicit.

In Implicit mode, append the json query parameter with any non-empty value to a WordPress page URL. (By convention, a value of 1 is used.) The content is then returned in JSON format rather than as an HTML page.

Here are a few examples of Implicit mode URLs:

http://www.mywordpresssite.net/?json=1
http://www.mywordpresssite.net/?p=99&json=1
http://www.mywordpresssite.net/tag/vegas/?json=1

In Explicit mode, set json to a method string. These may be found on the Settings page, which is available from the Settings > JSON API menu command.

Here are some examples of Explicit mode URLs:

http://www.mywordpresssite.net/?json=get_recent_posts
http://www.mywordpresssite.net/?json=get_post&post_id=99
http://www.mywordpresssite.net/?json=get_tag_posts&tag_slug=vegas

At the bottom of the JSON API Settings page, there is an API base field that sets a user-friendly permalink URL structure. By default, it is <yoursiteURL>/api/<method name>/<parameters>

Hence, the following permalinks would yield the same results as the above URLs:

http://www.mywordpresssite.net/api/get_recent_posts/
http://www.mywordpresssite.net/api/get_post/?post_id=99
http://www.mywordpresssite.net/api/get_tag_posts/?tag_slug=vegas

Core Methods

This group of methods include those that fetch data from your WordPress database. As such, they are the ones that you’ll probably be using the most. In fact, they are the only ones you’d use to make content available as a feed.

The remainder of this tutorial will provide an overview of a couple of core methods.

info()

It returns details on the JSON API plugin – specifically the status, version, and controllers. A controller is nothing more than a module that implements a related set of functionality. If supplied, the optional “controller” argument limits the returned information to a specific controller. The info() method is useful for obtaining a list of available methods at runtime. As mentioned previously, most of the methods are part of the Core controller. It’s the only one enabled by default so you’ll usually only see its methods in the results.

For the purposes of this tutorial, all output will be formatted for better readability, i.e. with line breaks.

// "http://www.mywordpresssite.net/api/info/" output:
{
  "status": "ok",
  "json_api_version": "1.1.1",
  "controllers": [
    "core",
    "gft"  //this is a custom controller that is not part of the plugin
  ]
}

// "http://www.mywordpresssite.net/api/info/?controller=core" output:
{
  "status": "ok",
  "name": "Core",
  "description": "Basic introspection methods",
  "methods": [
    "info",
    "get_recent_posts",
    "get_posts",
    "get_post",
    "get_page",
    "get_date_posts",
    "get_category_posts",
    "get_tag_posts",
    "get_author_posts",
    "get_search_results",
    "get_date_index",
    "get_category_index",
    "get_tag_index",
    "get_author_index",
    "get_page_index",
    "get_nonce"
  ]
}

get_recent_posts()

Returns an array of recent posts. You can invoke this from the WordPress home page either by setting the json parameter to a non-empty value (i.e., json=1) or from any page by setting json=get_recent_posts.

This method supports a few optional arguments as well:

  • count: determines how many posts per page are returned (the default value is 10)
  • page: return a specific page number from the results
  • post_type: used to retrieve custom post types
// "http://www.mywordpresssite.net/?json=get_recent_posts" output:
{
  "status": "ok",
  "count": 10,
  "count_total": 850,
  "pages": 87,
  "posts": [
    { ... },
    { ... },
    ...
  ]
}

// "http://www.mywordpresssite.net/?json=get_recent_posts&post_type=restaurant&page=10
// returns only the results of page 10 for type "restaurant"
{
  "status": "ok",
  "count": 10,
  "count_total": 679,
  "pages": 68,
  "posts": [
    { ... },
    { ... },
    ...
  ]
}

Conclusion

In today’s article we saw why the JSON API WordPress plugin get top marks for ease of use. In a future installment we’ll find out just how versatile it can be.

Rob Gravelle
Rob Gravelle
Rob Gravelle resides in Ottawa, Canada, and has been an IT guru for over 20 years. In that time, Rob has built systems for intelligence-related organizations such as Canada Border Services and various commercial businesses. In his spare time, Rob has become an accomplished music artist with several CDs and digital releases to his credit.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured