• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

dphiffer/wp-json-api: WordPress JSON API

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

dphiffer/wp-json-api

开源软件地址:

https://github.com/dphiffer/wp-json-api

开源编程语言:

PHP 100.0%

开源软件介绍:

JSON API

A RESTful API for WordPress

Description

JSON API allows you to retrieve and manipulate WordPress content using HTTP requests. There are three main goals:

  1. Provide a simple, consistent external interface
  2. Create a stable, understandable internal implementation
  3. 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.

See the Other Notes section for the complete documentation.

Installation

  1. Upload the json-api folder to the /wp-content/plugins/ directory or install directly through the plugin installer.
  2. Activate the plugin through the 'Plugins' menu in WordPress or by using the link provided by the plugin installer.

Screenshots

  1. Our old friend, in JSON format

Documentation

  1. General concepts
    1.1. Requests
    1.2. Controllers
    1.3. Responses
  2. Request methods
    2.1. Core controller methods
    2.2. Posts controller methods
    2.3. Respond controller methods
    2.4. Widgets controller methods
  3. Request arguments
    3.1. Output-modifying arguments
    3.2. Content-modifying arguments
    3.3. Using include/exclude and redirects
  4. Response objects
    4.1. Post response object
    4.2. Category response object
    4.3. Tag response object
    4.4. Author response object
    4.5. Comment response object
    4.6. Attachment response object
  5. Extending JSON API
    5.1. Plugin hooks
    5.2. Developing JSON API controllers
    5.3. Configuration options
  6. Unit tests
    6.1. Preparing a WordPress test site
    6.2. Running the tests

1. General Concepts

1.1. Requests

Requests use a simple REST-style 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:

  1. Implicit mode is triggered by setting the json query var to a non-empty value on any WordPress page. The content that would normally appear on that page is returned in JSON format.
  2. Explicit mode is triggered by setting json to a known method string. See Section 2: Request methods for a complete method listing.

Implicit mode examples:

  • http://www.example.org/?json=1
  • http://www.example.org/?p=47&json=1
  • http://www.example.org/tag/banana/?json=1

Explicit mode examples:

  • http://www.example.org/?json=get_recent_posts
  • http://www.example.org/?json=get_post&post_id=47
  • http://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=47
  • http://www.example.org/api/get_tag_posts/?tag_slug=banana

Further reading
See Section 3: Request arguments for more information about request arguments to modify the response.

1.2. 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 (core controller is implied, method is get_recent_posts)
  • http://www.example.org/api/info/ (core controller is implied)
  • http://www.example.org/api/core/get_category_posts/ (core controller can also be explicitly specified)
  • http://www.example.org/?json=respond.submit_comment (respond controller, submit_comment method)

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, and Respond. Developers are encouraged to suggest or submit additional controllers.

Further reading
See Section 2: Request methods for a complete reference of available controllers and methods. For documentation on extending JSON API with new controllers see Section 5.2: Developing JSON API controllers.

1.3. 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&#039;s comments. There you will have the option to edit or delete them.<\/p>\n",
          "parent": 0
        }
      ],
      "comment_count": 1,
      "comment_status": "open"
    }
  ]
}

2. 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

2.1. 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 results
  • post_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 results
  • post_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
  • id or post_id - set to the post's ID
  • slug or post_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
  • id or page_id - set to the page's ID
  • slug or page_slug - set to the page's URL slug

Optional arguments

  • children - set to a non-empty value to include a recursive hierarchy of child pages
  • post_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 format YYYY or YYYY-MM or YYYY-MM-DD (non-numeric characters are stripped from the var, so YYYYMMDD or YYYY/MM/DD are 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 results
  • post_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
  • id or category_id - set to the category's ID
  • slug or category_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 results
  • post_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
  • id or tag_id - set to the tag's ID
  • slug or tag_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 results
  • post_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
  • id or author_id - set to the author's ID
  • slug or author_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 results
  • post_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 results
  • post_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.

热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap