# Rendered Blocks
Source: https://developer.wordpress.org/rest-api/reference/rendered-blocks/
## Schema
The schema defines all the fields that exist within a Rendered Block record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `rendered` | The rendered block. JSON data type: string Context: `edit` |
|---|---|
## Create a Rendered Block
### Arguments
| `name` | Unique registered name for the block. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `edit` |
| `attributes` | Attributes for the block. |
| `post_id` | ID of the post context. |
### Definition
`POST /wp/v2/block-renderer/`
---
# Search Results
Source: https://developer.wordpress.org/rest-api/reference/search-results/
## Schema
The schema defines all the fields that exist within a search result record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | Unique identifier for the object. JSON data type: integer or string Read only Context: `view`, `embed` |
|---|---|
| `title` | The title for the object. JSON data type: string Read only Context: `view`, `embed` |
| `url` | URL to the object. JSON data type: string, Format: uri Read only Context: `view`, `embed` |
| `type` | Object type. JSON data type: string Read only Context: `view`, `embed` One of: `post`, `term`, `post-format` |
| `subtype` | Object subtype. JSON data type: string Read only Context: `view`, `embed` One of: `post`, `page`, `category`, `post_tag` |
## List Search Results
Query this endpoint to retrieve a collection of search results. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/search`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/search`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `10` |
| `search` | Limit results to those matching a string. |
| `type` | Limit results to items of an object type. Default: `post` One of: `post`, `term`, `post-format` |
| `subtype` | Limit results to items of one or more object subtypes. Default: `any` |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
---
# Themes
Source: https://developer.wordpress.org/rest-api/reference/themes/
## Schema
The schema defines all the fields that exist within a theme record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `stylesheet` | The theme's stylesheet. This uniquely identifies the theme. JSON data type: string Read only Context: `` |
|---|---|
| `template` | The theme's template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme's stylesheet. JSON data type: string Read only Context: `` |
| `author` | The theme author. JSON data type: object Read only Context: `` |
| `author_uri` | The website of the theme author. JSON data type: object Read only Context: `` |
| `description` | A description of the theme. JSON data type: object Read only Context: `` |
| `is_block_theme` | Whether the theme is a block-based theme. JSON data type: boolean Read only Context: `` |
| `name` | The name of the theme. JSON data type: object Read only Context: `` |
| `requires_php` | The minimum PHP version required for the theme to work. JSON data type: string Read only Context: `` |
| `requires_wp` | The minimum WordPress version required for the theme to work. JSON data type: string Read only Context: `` |
| `screenshot` | The theme's screenshot URL. JSON data type: string, Format: uri Read only Context: `` |
| `tags` | Tags indicating styles and features of the theme. JSON data type: object Read only Context: `` |
| `textdomain` | The theme's text domain. JSON data type: string Read only Context: `` |
| `theme_supports` | Features supported by this theme. JSON data type: object Read only Context: `` |
| `theme_uri` | The URI of the theme's webpage. JSON data type: object Read only Context: `` |
| `version` | The theme's current version. JSON data type: string Read only Context: `` |
| `status` | A named status for the theme. JSON data type: string Context: `` One of: `inactive`, `active` |
## Retrieve a Theme
### Definition & Example Request
`GET /wp/v2/themes`
Query this endpoint to retrieve a specific theme record.
`$ curl https://example.com/wp-json/wp/v2/themes`
### Arguments
| `status` | Limit result set to themes assigned one or more statuses. |
|---|---|
## Retrieve a Theme
### Definition & Example Request
`GET /wp/v2/themes/?)`
Query this endpoint to retrieve a specific theme record.
`$ curl https://example.com/wp-json/wp/v2/themes/?)`
### Arguments
| `stylesheet` | The theme's stylesheet. This uniquely identifies the theme. |
|---|---|
---
# Block Revisions
Source: https://developer.wordpress.org/rest-api/reference/block-revisions/
## Schema
The schema defines all the fields that exist within a Block Revision record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `author` | The ID for the author of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `date` | The date the revision was published, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
| `date_gmt` | The date the revision was published, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | The globally unique identifier for the post. JSON data type: object Read only Context: `view`, `edit` |
| `id` | Unique identifier for the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the revision was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `modified_gmt` | The date the revision was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `parent` | The ID for the parent of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `slug` | An alphanumeric identifier for the revision unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `title` | The title for the post. JSON data type: object Context: `view`, `edit`, `embed` |
| `content` | The content for the post. JSON data type: object Context: `view`, `edit` |
## List Block Revisions
Query this endpoint to retrieve a collection of Block Revisions. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/blocks//revisions`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/blocks//revisions`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. |
| `search` | Limit results to those matching a string. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by object attribute. Default: `date` One of: `date`, `id`, `include`, `relevance`, `slug`, `include_slugs`, `title` |
## Retrieve a Block Revision
### Definition & Example Request
`GET /wp/v2/blocks//revisions/`
Query this endpoint to retrieve a specific Block Revision record.
`$ curl https://example.com/wp-json/wp/v2/blocks//revisions/`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Delete a Block Revision
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `force` | Required to be true, as revisions do not support trashing. |
### Definition
`DELETE /wp/v2/blocks//revisions/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/blocks//revisions/`
## Retrieve a Block Revision
### Definition & Example Request
`GET /wp/v2/blocks//autosaves`
Query this endpoint to retrieve a specific Block Revision record.
`$ curl https://example.com/wp-json/wp/v2/blocks//autosaves`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Create a Block Revision
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `date` | The date the post was published, in the site's timezone. |
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `meta` | Meta fields. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/blocks//autosaves`
## Retrieve a Block Revision
### Definition & Example Request
`GET /wp/v2/blocks//autosaves/`
Query this endpoint to retrieve a specific Block Revision record.
`$ curl https://example.com/wp-json/wp/v2/blocks//autosaves/`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `id` | The ID for the autosave. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Editor Blocks
Source: https://developer.wordpress.org/rest-api/reference/blocks/
## Schema
The schema defines all the fields that exist within a Editor Block record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `date` | The date the post was published, in the site's timezone. JSON data type: string or null, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
|---|---|
| `date_gmt` | The date the post was published, as GMT. JSON data type: string or null, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | The globally unique identifier for the post. JSON data type: object Read only Context: `view`, `edit` |
| `id` | Unique identifier for the post. JSON data type: integer Read only Context: `view`, `edit`, `embed` |
| `link` | URL to the post. JSON data type: string, Format: uri Read only Context: `view`, `edit`, `embed` |
| `modified` | The date the post was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `modified_gmt` | The date the post was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `slug` | An alphanumeric identifier for the post unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `status` | A named status for the post. JSON data type: string Context: `view`, `edit` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `type` | Type of post. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `password` | A password to protect access to the content and excerpt. JSON data type: string Context: `edit` |
| `title` | The title for the post. JSON data type: object Context: `view`, `edit`, `embed` |
| `content` | The content for the post. JSON data type: object Context: `view`, `edit` |
| `meta` | Meta fields. JSON data type: object Context: `view`, `edit` |
| `template` | The theme file to use to display the post. JSON data type: string Context: `view`, `edit` |
## List Editor Blocks
Query this endpoint to retrieve a collection of Editor Blocks. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/blocks`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/blocks`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `10` |
| `search` | Limit results to those matching a string. |
| `after` | Limit response to posts published after a given ISO8601 compliant date. |
| `modified_after` | Limit response to posts modified after a given ISO8601 compliant date. |
| `before` | Limit response to posts published before a given ISO8601 compliant date. |
| `modified_before` | Limit response to posts modified before a given ISO8601 compliant date. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by post attribute. Default: `date` One of: `author`, `date`, `id`, `include`, `modified`, `parent`, `relevance`, `slug`, `include_slugs`, `title` |
| `search_columns` | Array of column names to be searched. |
| `slug` | Limit result set to posts with one or more specific slugs. |
| `status` | Limit result set to posts assigned one or more statuses. Default: `publish` |
## Create a Editor Block
### Arguments
| `date` | The date the post was published, in the site's timezone. |
|---|---|
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `meta` | Meta fields. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/blocks`
## Retrieve a Editor Block
### Definition & Example Request
`GET /wp/v2/blocks/`
Query this endpoint to retrieve a specific Editor Block record.
`$ curl https://example.com/wp-json/wp/v2/blocks/`
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `password` | The password for the post if it is password protected. |
## Update a Editor Block
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `date` | The date the post was published, in the site's timezone. |
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `meta` | Meta fields. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/blocks/`
### Example Request
``
## Delete a Editor Block
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `force` | Whether to bypass Trash and force deletion. |
### Definition
`DELETE /wp/v2/blocks/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/blocks/`
---
# Key Concepts
Source: https://developer.wordpress.org/rest-api/key-concepts/
In this page we’ll break down some of the key concepts and terms associated with the REST API: **Routes & Endpoints,** **Requests**, **Responses**, **Schema**, and **Controller Classes**. Each of these concepts play a crucial role in understanding, using, and extending the WordPress REST API, and each is explored in greater depth within this handbook.
## Routes & Endpoints
In the context of the WordPress REST API a **route** is a URI which can be mapped to different HTTP methods. The mapping of an individual HTTP method to a route is known as an **endpoint**.
As an example, if we make a `GET` request to the URI `http://oursite.com/wp-json/` we are returned a JSON response showing what routes are available, and what endpoints are available within each route.
`/wp-json/` is a route, and when that route receives a `GET` request then that request is handled by the endpoint which displays what is known as the index for the WordPress REST API.
The route `wp-json/wp/v2/posts` by contrast has a `GET` endpoint which returns a list of posts, but also a `POST` endpoint which accepts authenticated requests to create new posts.
We will learn how to register our own routes and endpoints in the following sections.
If you are using [non-pretty permalinks](https://wordpress.org/support/article/using-permalinks/), you should pass the REST API route as a query string parameter. The route `http://oursite.com/wp-json/` in the example above would hence be `http://oursite.com/?rest_route=/`.
If you get a `404` error when trying to access `http://oursite.com/wp-json/`, consider enabling pretty permalinks or try using the `rest_route` parameter instead.
## Requests
A REST API request is represented within WordPress by an instance of the `WP_REST_Request` class, which is used to store and retrieve information for the current request. A `WP_REST_Request` object is automatically generated when you make an HTTP request to a registered API route.
The data specified in this object (derived from the route URI or the JSON payload sent as a part of the request) determines what response you will get back out of the API.
Requests are usually submitted remotely via HTTP but may also be made internally from PHP within WordPress plugin or theme code. There are a lot of neat things you can do using this class, detailed further elsewhere in the handbook.
## Responses
Responses are the data you get back from the API. The `WP_REST_Response` class provides a way to interact with the response data returned by endpoints. Responses return the requested data, or can also be used to return errors if something goes wrong while fulfilling the request.
## Schema
Each endpoint requires a particular structure of input data, and returns data using a defined and predictable structure. Those data structures are defined in the API Schema.
The schema structures API data and provides a comprehensive list of all of the properties the API can return and which input parameters it can accept.
Well-defined schema also provides one layer of security within the API, as it enables us to validate and sanitize the requests being made to the API. The [Schema section](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/) explores this large topic further.
## Controller Classes
Controller classes unify and coordinate all these various moving parts within a REST API response cycle. With a controller class you can manage the registration of routes & endpoints, handle requests, utilize schema, and generate API responses.
A single class usually contains all of the logic for a given route, and a given route usually represents a specific type of data object within your WordPress site (like a custom post type or taxonomy).
---
# Block Directory Items
Source: https://developer.wordpress.org/rest-api/reference/block-directory-items/
## Schema
The schema defines all the fields that exist within a block directory item record. Any response from these endpoints can be expected to contain the fields below unless the `_filter` query parameter is used or the schema field only appears in a specific context. | `name` | The block name, in namespace/block-name format. JSON data type: string Context: `view` |
|---|---|
| `title` | The block title, in human readable format. JSON data type: string Context: `view` |
| `description` | A short description of the block, in human readable format. JSON data type: string Context: `view` |
| `id` | The block slug. JSON data type: string Context: `view` |
| `rating` | The star rating of the block. JSON data type: number Context: `view` |
| `rating_count` | The number of ratings. JSON data type: integer Context: `view` |
| `active_installs` | The number sites that have activated this block. JSON data type: integer Context: `view` |
| `author_block_rating` | The average rating of blocks published by the same author. JSON data type: number Context: `view` |
| `author_block_count` | The number of blocks published by the same author. JSON data type: integer Context: `view` |
| `author` | The WordPress.org username of the block author. JSON data type: string Context: `view` |
| `icon` | The block icon. JSON data type: string, Format: uri Context: `view` |
| `last_updated` | The date when the block was last updated. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view` |
| `humanized_updated` | The date when the block was last updated, in fuzzy human readable format. JSON data type: string Context: `view` |
## List Block Directory Items
Query this endpoint to retrieve a collection of block directory items. The response you receive can be controlled and filtered using the URL query parameters below. ### Definition
`GET /wp/v2/block-directory/search`### Example Request
`$ curl https://example.com/wp-json/wp/v2/block-directory/search`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `10` |
| `term` | Limit result set to blocks matching the search term. Required: 1 |
---
# Block Types
Source: https://developer.wordpress.org/rest-api/reference/block-types/
## Schema
The schema defines all the fields that exist within a block type record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `api_version` | Version of block API. JSON data type: integer Read only Context: `embed`, `view`, `edit` |
|---|---|
| `title` | Title of block type. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `name` | Unique name identifying the block type. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `description` | Description of block type. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `icon` | Icon of block type. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `attributes` | Block attributes. JSON data type: object or null Read only Context: `embed`, `view`, `edit` |
| `provides_context` | Context provided by blocks of this type. JSON data type: object Read only Context: `embed`, `view`, `edit` |
| `uses_context` | Context values inherited by blocks of this type. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `selectors` | Custom CSS selectors. JSON data type: object Read only Context: `embed`, `view`, `edit` |
| `supports` | Block supports. JSON data type: object Read only Context: `embed`, `view`, `edit` |
| `category` | Block category. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `is_dynamic` | Is the block dynamically rendered. JSON data type: boolean Read only Context: `embed`, `view`, `edit` |
| `editor_script_handles` | Editor script handles. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `script_handles` | Public facing and editor script handles. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `view_script_handles` | Public facing script handles. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `editor_style_handles` | Editor style handles. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `style_handles` | Public facing and editor style handles. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `styles` | Block style variations. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `variations` | Block variations. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `textdomain` | Public text domain. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `parent` | Parent blocks. JSON data type: array or null Read only Context: `embed`, `view`, `edit` |
| `ancestor` | Ancestor blocks. JSON data type: array or null Read only Context: `embed`, `view`, `edit` |
| `keywords` | Block keywords. JSON data type: array Read only Context: `embed`, `view`, `edit` |
| `example` | Block example. JSON data type: object or null Read only Context: `embed`, `view`, `edit` |
| `editor_script` | Editor script handle. DEPRECATED: Use `editor\_script\_handles` instead. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `script` | Public facing and editor script handle. DEPRECATED: Use `script\_handles` instead. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `view_script` | Public facing script handle. DEPRECATED: Use `view\_script\_handles` instead. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `editor_style` | Editor style handle. DEPRECATED: Use `editor\_style\_handles` instead. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
| `style` | Public facing and editor style handle. DEPRECATED: Use `style\_handles` instead. JSON data type: string or null Read only Context: `embed`, `view`, `edit` |
## Retrieve a Block Type
### Definition & Example Request
`GET /wp/v2/block-types`
Query this endpoint to retrieve a specific block type record.
`$ curl https://example.com/wp-json/wp/v2/block-types`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `namespace` | Block namespace. |
## Retrieve a Block Type
### Definition & Example Request
`GET /wp/v2/block-types/`
Query this endpoint to retrieve a specific block type record.
`$ curl https://example.com/wp-json/wp/v2/block-types/`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `namespace` | Block namespace. |
## Retrieve a Block Type
### Definition & Example Request
`GET /wp/v2/block-types//`
Query this endpoint to retrieve a specific block type record.
`$ curl https://example.com/wp-json/wp/v2/block-types//`
### Arguments
| `name` | Block name. |
|---|---|
| `namespace` | Block namespace. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Plugins
Source: https://developer.wordpress.org/rest-api/reference/plugins/
## Schema
The schema defines all the fields that exist within a plugin record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `plugin` | The plugin file. JSON data type: string Read only Context: `view`, `edit`, `embed` |
|---|---|
| `status` | The plugin activation status. JSON data type: string Context: `view`, `edit`, `embed` One of: `inactive`, `active` |
| `name` | The plugin name. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `plugin_uri` | The plugin's website address. JSON data type: string, Format: uri Read only Context: `view`, `edit` |
| `author` | The plugin author. JSON data type: object Read only Context: `view`, `edit` |
| `author_uri` | Plugin author's website address. JSON data type: string, Format: uri Read only Context: `view`, `edit` |
| `description` | The plugin description. JSON data type: object Read only Context: `view`, `edit` |
| `version` | The plugin version number. JSON data type: string Read only Context: `view`, `edit` |
| `network_only` | Whether the plugin can only be activated network-wide. JSON data type: boolean Read only Context: `view`, `edit`, `embed` |
| `requires_wp` | Minimum required version of WordPress. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `requires_php` | Minimum required version of PHP. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `textdomain` | The plugin's text domain. JSON data type: string Read only Context: `view`, `edit` |
## Retrieve a Plugin
### Definition & Example Request
`GET /wp/v2/plugins`
Query this endpoint to retrieve a specific plugin record.
`$ curl https://example.com/wp-json/wp/v2/plugins`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `search` | Limit results to those matching a string. |
| `status` | Limits results to plugins with the given status. |
## Create a Plugin
### Arguments
| `slug` | WordPress.org plugin directory slug. Required: 1 |
|---|---|
| `status` | The plugin activation status. Default: `inactive` One of: `inactive`, `active` |
### Definition
`POST /wp/v2/plugins`
## Retrieve a Plugin
### Definition & Example Request
`GET /wp/v2/plugins/?)`
Query this endpoint to retrieve a specific plugin record.
`$ curl https://example.com/wp-json/wp/v2/plugins/?)`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `plugin` | |
## Update a Plugin
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `plugin` | |
| `status` | The plugin activation status. One of: `inactive`, `active` |
### Definition
`POST /wp/v2/plugins/?)`
### Example Request
``
## Delete a Plugin
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `plugin` | |
### Definition
`DELETE /wp/v2/plugins/?)`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/plugins/?)`
---
# Application Passwords
Source: https://developer.wordpress.org/rest-api/reference/application-passwords/
## Schema
The schema defines all the fields that exist within a application password record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `uuid` | The unique identifier for the application password. JSON data type: string, Format: uuid Read only Context: `view`, `edit`, `embed` |
|---|---|
| `app_id` | A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace. JSON data type: string, Format: uuid Context: `view`, `edit`, `embed` |
| `name` | The name of the application password. JSON data type: string Context: `view`, `edit`, `embed` |
| `password` | The generated password. Only available after adding an application. JSON data type: string Read only Context: `edit` |
| `created` | The GMT date the application password was created. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `last_used` | The GMT date the application password was last used. JSON data type: string or null, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `last_ip` | The IP address the application password was last used by. JSON data type: string or null, Format: ip Read only Context: `view`, `edit` |
## Retrieve a Application Password
### Definition & Example Request
`GET /wp/v2/users/)/application-passwords`
Query this endpoint to retrieve a specific application password record.
`$ curl https://example.com/wp-json/wp/v2/users/)/application-passwords`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Create a Application Password
### Arguments
| `app_id` | A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace. |
|---|---|
| `name` | The name of the application password. Required: 1 |
### Definition
`POST /wp/v2/users/)/application-passwords`
## Delete a Application Password
There are no arguments for this endpoint.
### Definition
`DELETE /wp/v2/users/)/application-passwords`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/users/)/application-passwords`
## Retrieve a Application Password
### Definition & Example Request
`GET /wp/v2/users/)/application-passwords/introspect`
Query this endpoint to retrieve a specific application password record.
`$ curl https://example.com/wp-json/wp/v2/users/)/application-passwords/introspect`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Retrieve a Application Password
### Definition & Example Request
`GET /wp/v2/users/)/application-passwords/`
Query this endpoint to retrieve a specific application password record.
`$ curl https://example.com/wp-json/wp/v2/users/)/application-passwords/`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Update a Application Password
### Arguments
| `app_id` | A UUID provided by the application to uniquely identify it. It is recommended to use an UUID v5 with the URL or DNS namespace. |
|---|---|
| `name` | The name of the application password. |
### Definition
`POST /wp/v2/users/)/application-passwords/`
### Example Request
``
## Delete a Application Password
There are no arguments for this endpoint.
### Definition
`DELETE /wp/v2/users/)/application-passwords/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/users/)/application-passwords/`
---
# Block Pattern Categories
Source: https://developer.wordpress.org/rest-api/reference/block-pattern-categories/
## Schema
The schema defines all the fields that exist within a block pattern category record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `name` | The category name. JSON data type: string Read only Context: `view`, `edit`, `embed` |
|---|---|
| `label` | The category label, in human readable format. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `description` | The category description, in human readable format. JSON data type: string Read only Context: `view`, `edit`, `embed` |
## Retrieve a Block Pattern Category
### Definition & Example Request
`GET /wp/v2/block-patterns/categories`
Query this endpoint to retrieve a specific block pattern category record.
`$ curl https://example.com/wp-json/wp/v2/block-patterns/categories`
There are no arguments for this endpoint.
---
# Block Patterns
Source: https://developer.wordpress.org/rest-api/reference/block-patterns/
## Schema
The schema defines all the fields that exist within a block pattern record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `name` | The pattern name. JSON data type: string Read only Context: `view`, `edit`, `embed` |
|---|---|
| `title` | The pattern title, in human readable format. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `content` | The pattern content. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `description` | The pattern detailed description. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `viewport_width` | The pattern viewport width for inserter preview. JSON data type: number Read only Context: `view`, `edit`, `embed` |
| `inserter` | Determines whether the pattern is visible in inserter. JSON data type: boolean Read only Context: `view`, `edit`, `embed` |
| `categories` | The pattern category slugs. JSON data type: array Read only Context: `view`, `edit`, `embed` |
| `keywords` | The pattern keywords. JSON data type: array Read only Context: `view`, `edit`, `embed` |
| `block_types` | Block types that the pattern is intended to be used with. JSON data type: array Read only Context: `view`, `edit`, `embed` |
| `post_types` | An array of post types that the pattern is restricted to be used with. JSON data type: array Read only Context: `view`, `edit`, `embed` |
| `template_types` | An array of template types where the pattern fits. JSON data type: array Read only Context: `view`, `edit`, `embed` |
| `source` | Where the pattern comes from e.g. core JSON data type: string Read only Context: `view`, `edit`, `embed` One of: `core`, `plugin`, `theme`, `pattern-directory/core`, `pattern-directory/theme`, `pattern-directory/featured` |
## Retrieve a Block Pattern
### Definition & Example Request
`GET /wp/v2/block-patterns/patterns`
Query this endpoint to retrieve a specific block pattern record.
`$ curl https://example.com/wp-json/wp/v2/block-patterns/patterns`
There are no arguments for this endpoint.
---
# Menu Locations
Source: https://developer.wordpress.org/rest-api/reference/menu-locations/
## Schema
The schema defines all the fields that exist within a menu location record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `name` | The name of the menu location. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `description` | The description of the menu location. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `menu` | The ID of the assigned menu. JSON data type: integer Read only Context: `embed`, `view`, `edit` |
## Retrieve a Menu Location
### Definition & Example Request
`GET /wp/v2/menu-locations`
Query this endpoint to retrieve a specific menu location record.
`$ curl https://example.com/wp-json/wp/v2/menu-locations`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Retrieve a Menu Location
### Definition & Example Request
`GET /wp/v2/menu-locations/`
Query this endpoint to retrieve a specific menu location record.
`$ curl https://example.com/wp-json/wp/v2/menu-locations/`
### Arguments
| `location` | An alphanumeric identifier for the menu location. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Nav_Menu_Item Revisions
Source: https://developer.wordpress.org/rest-api/reference/nav_menu_item-revisions/
## Schema
The schema defines all the fields that exist within a nav\_menu\_item revision record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `author` | The ID for the author of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `date` | The date the revision was published, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
| `date_gmt` | The date the revision was published, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | GUID for the revision, as it exists in the database. JSON data type: string Context: `view`, `edit` |
| `id` | Unique identifier for the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the revision was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `modified_gmt` | The date the revision was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `parent` | The ID for the parent of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `slug` | An alphanumeric identifier for the revision unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `title` | The title for the object. JSON data type: string or object Context: `view`, `edit`, `embed` |
| `preview_link` | Preview link for the post. JSON data type: string, Format: uri Read only Context: `edit` |
## Retrieve a Nav\_Menu\_Item Revision
### Definition & Example Request
`GET /wp/v2/menu-items//autosaves`
Query this endpoint to retrieve a specific nav\_menu\_item revision record.
`$ curl https://example.com/wp-json/wp/v2/menu-items//autosaves`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Create a Nav\_Menu\_Item Revision
### Arguments
| `parent` | The ID for the parent of the object. |
|---|---|
| `title` | The title for the object. |
| `type` | The family of objects originally represented, such as "post\_type" or "taxonomy". One of: `taxonomy`, `post_type`, `post_type_archive`, `custom` |
| `status` | A named status for the object. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `attr_title` | Text for the title attribute of the link element for this menu item. |
| `classes` | Class names for the link element of this menu item. |
| `description` | The description of this menu item. |
| `menu_order` | The DB ID of the nav\_menu\_item that is this item's menu parent, if any, otherwise 0. |
| `object` | The type of object originally represented, such as "category", "post", or "attachment". |
| `object_id` | The database ID of the original object this menu item represents, for example the ID for posts or the term\_id for categories. |
| `target` | The target attribute of the link element for this menu item. One of: `_blank`, `` |
| `url` | The URL to which this menu item points. |
| `xfn` | The XFN relationship expressed in the link of this menu item. |
| `menus` | The terms assigned to the object in the nav\_menu taxonomy. |
| `meta` | Meta fields. |
### Definition
`POST /wp/v2/menu-items//autosaves`
## Retrieve a Nav\_Menu\_Item Revision
### Definition & Example Request
`GET /wp/v2/menu-items//autosaves/`
Query this endpoint to retrieve a specific nav\_menu\_item revision record.
`$ curl https://example.com/wp-json/wp/v2/menu-items//autosaves/`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `id` | The ID for the autosave. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Nav_Menu_Items
Source: https://developer.wordpress.org/rest-api/reference/nav_menu_items/
## Schema
The schema defines all the fields that exist within a nav\_menu\_item record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `title` | The title for the object. JSON data type: string or object Context: `view`, `edit`, `embed` |
|---|---|
| `id` | Unique identifier for the object. JSON data type: integer Read only Context: `view`, `edit`, `embed` |
| `type_label` | The singular label used to describe this type of menu item. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `type` | The family of objects originally represented, such as "post\_type" or "taxonomy". JSON data type: string Context: `view`, `edit`, `embed` One of: `taxonomy`, `post_type`, `post_type_archive`, `custom` |
| `status` | A named status for the object. JSON data type: string Context: `view`, `edit`, `embed` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `parent` | The ID for the parent of the object. JSON data type: integer Context: `view`, `edit`, `embed` |
| `attr_title` | Text for the title attribute of the link element for this menu item. JSON data type: string Context: `view`, `edit`, `embed` |
| `classes` | Class names for the link element of this menu item. JSON data type: array Context: `view`, `edit`, `embed` |
| `description` | The description of this menu item. JSON data type: string Context: `view`, `edit`, `embed` |
| `menu_order` | The DB ID of the nav\_menu\_item that is this item's menu parent, if any, otherwise 0. JSON data type: integer Context: `view`, `edit`, `embed` |
| `object` | The type of object originally represented, such as "category", "post", or "attachment". JSON data type: string Context: `view`, `edit`, `embed` |
| `object_id` | The database ID of the original object this menu item represents, for example the ID for posts or the term\_id for categories. JSON data type: integer Context: `view`, `edit`, `embed` |
| `target` | The target attribute of the link element for this menu item. JSON data type: string Context: `view`, `edit`, `embed` One of: `_blank`, `` |
| `url` | The URL to which this menu item points. JSON data type: string, Format: uri Context: `view`, `edit`, `embed` |
| `xfn` | The XFN relationship expressed in the link of this menu item. JSON data type: array Context: `view`, `edit`, `embed` |
| `invalid` | Whether the menu item represents an object that no longer exists. JSON data type: boolean Read only Context: `view`, `edit`, `embed` |
| `menus` | The terms assigned to the object in the nav\_menu taxonomy. JSON data type: integer Context: `view`, `edit` |
| `meta` | Meta fields. JSON data type: object Context: `view`, `edit` |
## List Nav\_Menu\_Items
Query this endpoint to retrieve a collection of nav\_menu\_items. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/menu-items`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/menu-items`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `100` |
| `search` | Limit results to those matching a string. |
| `after` | Limit response to posts published after a given ISO8601 compliant date. |
| `modified_after` | Limit response to posts modified after a given ISO8601 compliant date. |
| `before` | Limit response to posts published before a given ISO8601 compliant date. |
| `modified_before` | Limit response to posts modified before a given ISO8601 compliant date. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `asc` One of: `asc`, `desc` |
| `orderby` | Sort collection by object attribute. Default: `menu_order` One of: `author`, `date`, `id`, `include`, `modified`, `parent`, `relevance`, `slug`, `include_slugs`, `title`, `menu_order` |
| `search_columns` | Array of column names to be searched. |
| `slug` | Limit result set to posts with one or more specific slugs. |
| `status` | Limit result set to posts assigned one or more statuses. Default: `publish` |
| `tax_relation` | Limit result set based on relationship between multiple taxonomies. One of: `AND`, `OR` |
| `menus` | Limit result set to items with specific terms assigned in the menus taxonomy. |
| `menus_exclude` | Limit result set to items except those with specific terms assigned in the menus taxonomy. |
| `menu_order` | Limit result set to posts with a specific menu\_order value. |
## Create a Nav\_Menu\_Item
### Arguments
| `title` | The title for the object. |
|---|---|
| `type` | The family of objects originally represented, such as "post\_type" or "taxonomy". Default: `custom` One of: `taxonomy`, `post_type`, `post_type_archive`, `custom` |
| `status` | A named status for the object. Default: `publish` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `parent` | The ID for the parent of the object. |
| `attr_title` | Text for the title attribute of the link element for this menu item. |
| `classes` | Class names for the link element of this menu item. |
| `description` | The description of this menu item. |
| `menu_order` | The DB ID of the nav\_menu\_item that is this item's menu parent, if any, otherwise 0. Default: `1` |
| `object` | The type of object originally represented, such as "category", "post", or "attachment". |
| `object_id` | The database ID of the original object this menu item represents, for example the ID for posts or the term\_id for categories. |
| `target` | The target attribute of the link element for this menu item. One of: `_blank`, `` |
| `url` | The URL to which this menu item points. |
| `xfn` | The XFN relationship expressed in the link of this menu item. |
| `menus` | The terms assigned to the object in the nav\_menu taxonomy. |
| `meta` | Meta fields. |
### Definition
`POST /wp/v2/menu-items`
## Retrieve a Nav\_Menu\_Item
### Definition & Example Request
`GET /wp/v2/menu-items/`
Query this endpoint to retrieve a specific nav\_menu\_item record.
`$ curl https://example.com/wp-json/wp/v2/menu-items/`
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Update a Nav\_Menu\_Item
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `title` | The title for the object. |
| `type` | The family of objects originally represented, such as "post\_type" or "taxonomy". One of: `taxonomy`, `post_type`, `post_type_archive`, `custom` |
| `status` | A named status for the object. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `parent` | The ID for the parent of the object. |
| `attr_title` | Text for the title attribute of the link element for this menu item. |
| `classes` | Class names for the link element of this menu item. |
| `description` | The description of this menu item. |
| `menu_order` | The DB ID of the nav\_menu\_item that is this item's menu parent, if any, otherwise 0. |
| `object` | The type of object originally represented, such as "category", "post", or "attachment". |
| `object_id` | The database ID of the original object this menu item represents, for example the ID for posts or the term\_id for categories. |
| `target` | The target attribute of the link element for this menu item. One of: `_blank`, `` |
| `url` | The URL to which this menu item points. |
| `xfn` | The XFN relationship expressed in the link of this menu item. |
| `menus` | The terms assigned to the object in the nav\_menu taxonomy. |
| `meta` | Meta fields. |
### Definition
`POST /wp/v2/menu-items/`
### Example Request
``
## Delete a Nav\_Menu\_Item
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `force` | Whether to bypass Trash and force deletion. |
### Definition
`DELETE /wp/v2/menu-items/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/menu-items/`
---
# Nav_Menus
Source: https://developer.wordpress.org/rest-api/reference/nav_menus/
## Schema
The schema defines all the fields that exist within a nav\_menu record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | Unique identifier for the term. JSON data type: integer Read only Context: `view`, `embed`, `edit` |
|---|---|
| `description` | HTML description of the term. JSON data type: string Context: `view`, `edit` |
| `name` | HTML title for the term. JSON data type: string Context: `view`, `embed`, `edit` |
| `slug` | An alphanumeric identifier for the term unique to its type. JSON data type: string Context: `view`, `embed`, `edit` |
| `meta` | Meta fields. JSON data type: object Context: `view`, `edit` |
| `locations` | The locations assigned to the menu. JSON data type: array Context: `view`, `edit` |
| `auto_add` | Whether to automatically add top level pages to this menu. JSON data type: boolean Context: `view`, `edit` |
## List Nav\_Menus
Query this endpoint to retrieve a collection of nav\_menus. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/menus`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/menus`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `10` |
| `search` | Limit results to those matching a string. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `asc` One of: `asc`, `desc` |
| `orderby` | Sort collection by term attribute. Default: `name` One of: `id`, `include`, `name`, `slug`, `include_slugs`, `term_group`, `description`, `count` |
| `hide_empty` | Whether to hide terms not assigned to any posts. |
| `post` | Limit result set to terms assigned to a specific post. |
| `slug` | Limit result set to terms with one or more specific slugs. |
## Create a Nav\_Menu
### Arguments
| `description` | HTML description of the term. |
|---|---|
| `name` | HTML title for the term. Required: 1 |
| `slug` | An alphanumeric identifier for the term unique to its type. |
| `meta` | Meta fields. |
| `locations` | The locations assigned to the menu. |
| `auto_add` | Whether to automatically add top level pages to this menu. |
### Definition
`POST /wp/v2/menus`
## Retrieve a Nav\_Menu
### Definition & Example Request
`GET /wp/v2/menus/`
Query this endpoint to retrieve a specific nav\_menu record.
`$ curl https://example.com/wp-json/wp/v2/menus/`
### Arguments
| `id` | Unique identifier for the term. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Update a Nav\_Menu
### Arguments
| `id` | Unique identifier for the term. |
|---|---|
| `description` | HTML description of the term. |
| `name` | HTML title for the term. |
| `slug` | An alphanumeric identifier for the term unique to its type. |
| `meta` | Meta fields. |
| `locations` | The locations assigned to the menu. |
| `auto_add` | Whether to automatically add top level pages to this menu. |
### Definition
`POST /wp/v2/menus/`
### Example Request
``
## Delete a Nav\_Menu
### Arguments
| `id` | Unique identifier for the term. |
|---|---|
| `force` | Required to be true, as terms do not support trashing. |
### Definition
`DELETE /wp/v2/menus/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/menus/`
---
# Pattern Directory Items
Source: https://developer.wordpress.org/rest-api/reference/pattern-directory-items/
## Schema
The schema defines all the fields that exist within a pattern directory item record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | The pattern ID. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `title` | The pattern title, in human readable format. JSON data type: string Context: `view`, `edit`, `embed` |
| `content` | The pattern content. JSON data type: string Context: `view`, `edit`, `embed` |
| `categories` | The pattern's category slugs. JSON data type: array Context: `view`, `edit`, `embed` |
| `keywords` | The pattern's keywords. JSON data type: array Context: `view`, `edit`, `embed` |
| `description` | A description of the pattern. JSON data type: string Context: `view`, `edit`, `embed` |
| `viewport_width` | The preferred width of the viewport when previewing a pattern, in pixels. JSON data type: integer Context: `view`, `edit`, `embed` |
| `block_types` | The block types which can use this pattern. JSON data type: array Context: `view`, `embed` |
## List Pattern Directory Items
Query this endpoint to retrieve a collection of pattern directory items. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/pattern-directory/patterns`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/pattern-directory/patterns`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `100` |
| `search` | Limit results to those matching a string. |
| `category` | Limit results to those matching a category ID. |
| `keyword` | Limit results to those matching a keyword ID. |
| `slug` | Limit results to those matching a pattern (slug). |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by post attribute. Default: `date` One of: `author`, `date`, `id`, `include`, `modified`, `parent`, `relevance`, `slug`, `include_slugs`, `title`, `favorite_count` |
---
# Sidebars
Source: https://developer.wordpress.org/rest-api/reference/sidebars/
## Schema
The schema defines all the fields that exist within a sidebar record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | ID of sidebar. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `name` | Unique name identifying the sidebar. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `description` | Description of sidebar. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `class` | Extra CSS class to assign to the sidebar in the Widgets interface. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `before_widget` | HTML content to prepend to each widget's HTML output when assigned to this sidebar. Default is an opening list item element. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `after_widget` | HTML content to append to each widget's HTML output when assigned to this sidebar. Default is a closing list item element. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `before_title` | HTML content to prepend to the sidebar title when displayed. Default is an opening h2 element. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `after_title` | HTML content to append to the sidebar title when displayed. Default is a closing h2 element. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `status` | Status of sidebar. JSON data type: string Read only Context: `embed`, `view`, `edit` One of: `active`, `inactive` |
| `widgets` | Nested widgets. JSON data type: array Context: `embed`, `view`, `edit` |
## Retrieve a Sidebar
### Definition & Example Request
`GET /wp/v2/sidebars`
Query this endpoint to retrieve a specific sidebar record.
`$ curl https://example.com/wp-json/wp/v2/sidebars`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Retrieve a Sidebar
### Definition & Example Request
`GET /wp/v2/sidebars/`
Query this endpoint to retrieve a specific sidebar record.
`$ curl https://example.com/wp-json/wp/v2/sidebars/`
### Arguments
| `id` | The id of a registered sidebar |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Update a Sidebar
### Arguments
| `widgets` | Nested widgets. |
|---|---|
### Definition
`POST /wp/v2/sidebars/`
### Example Request
``
---
# Widget Types
Source: https://developer.wordpress.org/rest-api/reference/widget-types/
## Schema
The schema defines all the fields that exist within a widget type record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | Unique slug identifying the widget type. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `name` | Human-readable name identifying the widget type. JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `description` | Description of the widget. JSON data type: string Context: `view`, `edit`, `embed` |
| `is_multi` | Whether the widget supports multiple instances JSON data type: boolean Read only Context: `view`, `edit`, `embed` |
| `classname` | Class name JSON data type: string Read only Context: `embed`, `view`, `edit` |
## Retrieve a Widget Type
### Definition & Example Request
`GET /wp/v2/widget-types`
Query this endpoint to retrieve a specific widget type record.
`$ curl https://example.com/wp-json/wp/v2/widget-types`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Retrieve a Widget Type
### Definition & Example Request
`GET /wp/v2/widget-types/`
Query this endpoint to retrieve a specific widget type record.
`$ curl https://example.com/wp-json/wp/v2/widget-types/`
### Arguments
| `id` | The widget type id. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Widgets
Source: https://developer.wordpress.org/rest-api/reference/widgets/
## Schema
The schema defines all the fields that exist within a widget record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | Unique identifier for the widget. JSON data type: string Context: `view`, `edit`, `embed` |
|---|---|
| `id_base` | The type of the widget. Corresponds to ID in widget-types endpoint. JSON data type: string Context: `view`, `edit`, `embed` |
| `sidebar` | The sidebar the widget belongs to. JSON data type: string Context: `view`, `edit`, `embed` |
| `rendered` | HTML representation of the widget. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `rendered_form` | HTML representation of the widget admin form. JSON data type: string Read only Context: `edit` |
| `instance` | Instance settings of the widget, if supported. JSON data type: object Context: `edit` |
| `form_data` | URL-encoded form data from the widget admin form. Used to update a widget that does not support instance. Write only. JSON data type: string Context: `` |
## Retrieve a Widget
### Definition & Example Request
`GET /wp/v2/widgets`
Query this endpoint to retrieve a specific widget record.
`$ curl https://example.com/wp-json/wp/v2/widgets`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `sidebar` | The sidebar to return widgets for. |
## Create a Widget
### Arguments
| `id` | Unique identifier for the widget. |
|---|---|
| `id_base` | The type of the widget. Corresponds to ID in widget-types endpoint. |
| `sidebar` | The sidebar the widget belongs to. Required: 1 Default: `wp_inactive_widgets` |
| `instance` | Instance settings of the widget, if supported. |
| `form_data` | URL-encoded form data from the widget admin form. Used to update a widget that does not support instance. Write only. |
### Definition
`POST /wp/v2/widgets`
## Retrieve a Widget
### Definition & Example Request
`GET /wp/v2/widgets/`
Query this endpoint to retrieve a specific widget record.
`$ curl https://example.com/wp-json/wp/v2/widgets/`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
## Update a Widget
### Arguments
| `id` | Unique identifier for the widget. |
|---|---|
| `id_base` | The type of the widget. Corresponds to ID in widget-types endpoint. |
| `sidebar` | The sidebar the widget belongs to. |
| `instance` | Instance settings of the widget, if supported. |
| `form_data` | URL-encoded form data from the widget admin form. Used to update a widget that does not support instance. Write only. |
### Definition
`POST /wp/v2/widgets/`
### Example Request
``
## Delete a Widget
### Arguments
| `force` | Whether to force removal of the widget, or move it to the inactive sidebar. |
|---|---|
### Definition
`DELETE /wp/v2/widgets/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/widgets/`
---
# Wp Site Health Tests
Source: https://developer.wordpress.org/rest-api/reference/wp-site-health-tests/
## Schema
The schema defines all the fields that exist within a wp site health test record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `test` | The name of the test being run. JSON data type: string Read only Context: `` |
|---|---|
| `label` | A label describing the test. JSON data type: string Read only Context: `` |
| `status` | The status of the test. JSON data type: string Read only Context: `` One of: `good`, `recommended`, `critical` |
| `badge` | The category this test is grouped in. JSON data type: object Read only Context: `` |
| `description` | A more descriptive explanation of what the test looks for, and why it is important for the user. JSON data type: string Read only Context: `` |
| `actions` | HTML containing an action to direct the user to where they can resolve the issue. JSON data type: string Read only Context: `` |
## Retrieve a Wp Site Health Test
### Definition & Example Request
`GET /wp-site-health/v1/tests/background-updates`
Query this endpoint to retrieve a specific wp site health test record.
`$ curl https://example.com/wp-json/wp-site-health/v1/tests/background-updates`
There are no arguments for this endpoint.
## Retrieve a Wp Site Health Test
### Definition & Example Request
`GET /wp-site-health/v1/tests/loopback-requests`
Query this endpoint to retrieve a specific wp site health test record.
`$ curl https://example.com/wp-json/wp-site-health/v1/tests/loopback-requests`
There are no arguments for this endpoint.
## Retrieve a Wp Site Health Test
### Definition & Example Request
`GET /wp-site-health/v1/tests/https-status`
Query this endpoint to retrieve a specific wp site health test record.
`$ curl https://example.com/wp-json/wp-site-health/v1/tests/https-status`
There are no arguments for this endpoint.
## Retrieve a Wp Site Health Test
### Definition & Example Request
`GET /wp-site-health/v1/tests/dotorg-communication`
Query this endpoint to retrieve a specific wp site health test record.
`$ curl https://example.com/wp-json/wp-site-health/v1/tests/dotorg-communication`
There are no arguments for this endpoint.
## Retrieve a Wp Site Health Test
### Definition & Example Request
`GET /wp-site-health/v1/tests/authorization-header`
Query this endpoint to retrieve a specific wp site health test record.
`$ curl https://example.com/wp-json/wp-site-health/v1/tests/authorization-header`
There are no arguments for this endpoint.
---
# Global_Styles
Source: https://developer.wordpress.org/rest-api/reference/wp_global_styles/
## Schema
The schema defines all the fields that exist within a global\_styles record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | ID of global styles config. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `styles` | Global styles. JSON data type: object Context: `view`, `edit` |
| `settings` | Global settings. JSON data type: object Context: `view`, `edit` |
| `title` | Title of the global styles variation. JSON data type: object or string Context: `embed`, `view`, `edit` |
## Retrieve a Global\_Styles
### Definition & Example Request
`GET /wp/v2/global-styles/`
Query this endpoint to retrieve a specific global\_styles record.
`$ curl https://example.com/wp-json/wp/v2/global-styles/`
### Arguments
| `id` | The id of a template |
|---|---|
## Update a Global\_Styles
### Arguments
| `styles` | Global styles. |
|---|---|
| `settings` | Global settings. |
| `title` | Title of the global styles variation. |
### Definition
`POST /wp/v2/global-styles/`
### Example Request
``
---
# Navigation Revisions
Source: https://developer.wordpress.org/rest-api/reference/wp_navigation-revisions/
## Schema
The schema defines all the fields that exist within a navigation revision record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `author` | The ID for the author of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `date` | The date the revision was published, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
| `date_gmt` | The date the revision was published, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | The globally unique identifier for the post. JSON data type: object Read only Context: `view`, `edit` |
| `id` | Unique identifier for the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the revision was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `modified_gmt` | The date the revision was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `parent` | The ID for the parent of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `slug` | An alphanumeric identifier for the revision unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `title` | The title for the post. JSON data type: object Context: `view`, `edit`, `embed` |
| `content` | The content for the post. JSON data type: object Context: `view`, `edit`, `embed` |
## List Navigation Revisions
Query this endpoint to retrieve a collection of navigation revisions. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/navigation//revisions`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/navigation//revisions`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. |
| `search` | Limit results to those matching a string. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by object attribute. Default: `date` One of: `date`, `id`, `include`, `relevance`, `slug`, `include_slugs`, `title` |
## Retrieve a Navigation Revision
### Definition & Example Request
`GET /wp/v2/navigation//revisions/`
Query this endpoint to retrieve a specific navigation revision record.
`$ curl https://example.com/wp-json/wp/v2/navigation//revisions/`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Delete a Navigation Revision
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `force` | Required to be true, as revisions do not support trashing. |
### Definition
`DELETE /wp/v2/navigation//revisions/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/navigation//revisions/`
## Retrieve a Navigation Revision
### Definition & Example Request
`GET /wp/v2/navigation//autosaves`
Query this endpoint to retrieve a specific navigation revision record.
`$ curl https://example.com/wp-json/wp/v2/navigation//autosaves`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Create a Navigation Revision
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `date` | The date the post was published, in the site's timezone. |
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/navigation//autosaves`
## Retrieve a Navigation Revision
### Definition & Example Request
`GET /wp/v2/navigation//autosaves/`
Query this endpoint to retrieve a specific navigation revision record.
`$ curl https://example.com/wp-json/wp/v2/navigation//autosaves/`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `id` | The ID for the autosave. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Navigations
Source: https://developer.wordpress.org/rest-api/reference/wp_navigations/
## Schema
The schema defines all the fields that exist within a navigation record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `date` | The date the post was published, in the site's timezone. JSON data type: string or null, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
|---|---|
| `date_gmt` | The date the post was published, as GMT. JSON data type: string or null, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | The globally unique identifier for the post. JSON data type: object Read only Context: `view`, `edit` |
| `id` | Unique identifier for the post. JSON data type: integer Read only Context: `view`, `edit`, `embed` |
| `link` | URL to the post. JSON data type: string, Format: uri Read only Context: `view`, `edit`, `embed` |
| `modified` | The date the post was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `modified_gmt` | The date the post was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `slug` | An alphanumeric identifier for the post unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `status` | A named status for the post. JSON data type: string Context: `view`, `edit`, `embed` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `type` | Type of post. JSON data type: string Read only Context: `view`, `edit`, `embed` |
| `password` | A password to protect access to the content and excerpt. JSON data type: string Context: `edit` |
| `title` | The title for the post. JSON data type: object Context: `view`, `edit`, `embed` |
| `content` | The content for the post. JSON data type: object Context: `view`, `edit`, `embed` |
| `template` | The theme file to use to display the post. JSON data type: string Context: `view`, `edit` |
## List Navigations
Query this endpoint to retrieve a collection of navigations. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/navigation`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/navigation`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. Default: `10` |
| `search` | Limit results to those matching a string. |
| `after` | Limit response to posts published after a given ISO8601 compliant date. |
| `modified_after` | Limit response to posts modified after a given ISO8601 compliant date. |
| `before` | Limit response to posts published before a given ISO8601 compliant date. |
| `modified_before` | Limit response to posts modified before a given ISO8601 compliant date. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by post attribute. Default: `date` One of: `author`, `date`, `id`, `include`, `modified`, `parent`, `relevance`, `slug`, `include_slugs`, `title` |
| `search_columns` | Array of column names to be searched. |
| `slug` | Limit result set to posts with one or more specific slugs. |
| `status` | Limit result set to posts assigned one or more statuses. Default: `publish` |
## Create a Navigation
### Arguments
| `date` | The date the post was published, in the site's timezone. |
|---|---|
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/navigation`
## Retrieve a Navigation
### Definition & Example Request
`GET /wp/v2/navigation/`
Query this endpoint to retrieve a specific navigation record.
`$ curl https://example.com/wp-json/wp/v2/navigation/`
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `password` | The password for the post if it is password protected. |
## Update a Navigation
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `date` | The date the post was published, in the site's timezone. |
| `date_gmt` | The date the post was published, as GMT. |
| `slug` | An alphanumeric identifier for the post unique to its type. |
| `status` | A named status for the post. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `password` | A password to protect access to the content and excerpt. |
| `title` | The title for the post. |
| `content` | The content for the post. |
| `template` | The theme file to use to display the post. |
### Definition
`POST /wp/v2/navigation/`
### Example Request
``
## Delete a Navigation
### Arguments
| `id` | Unique identifier for the post. |
|---|---|
| `force` | Whether to bypass Trash and force deletion. |
### Definition
`DELETE /wp/v2/navigation/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/navigation/`
---
# Template Revisions
Source: https://developer.wordpress.org/rest-api/reference/wp_template-revisions/
## Schema
The schema defines all the fields that exist within a template revision record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `author` | The ID for the author of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `date` | The date the revision was published, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
| `date_gmt` | The date the revision was published, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | GUID for the revision, as it exists in the database. JSON data type: string Context: `view`, `edit` |
| `id` | Unique identifier for the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the revision was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `modified_gmt` | The date the revision was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `parent` | The ID for the parent of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `slug` | An alphanumeric identifier for the revision unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `title` | Title of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `content` | Content of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
## List Template Revisions
Query this endpoint to retrieve a collection of template revisions. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/templates//revisions`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/templates//revisions`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. |
| `search` | Limit results to those matching a string. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by object attribute. Default: `date` One of: `date`, `id`, `include`, `relevance`, `slug`, `include_slugs`, `title` |
## Retrieve a Template Revision
### Definition & Example Request
`GET /wp/v2/templates//revisions/`
Query this endpoint to retrieve a specific template revision record.
`$ curl https://example.com/wp-json/wp/v2/templates//revisions/`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Delete a Template Revision
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `force` | Required to be true, as revisions do not support trashing. |
### Definition
`DELETE /wp/v2/templates//revisions/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/templates//revisions/`
## Retrieve a Template Revision
### Definition & Example Request
`GET /wp/v2/templates//autosaves`
Query this endpoint to retrieve a specific template revision record.
`$ curl https://example.com/wp-json/wp/v2/templates//autosaves`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Create a Template Revision
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `slug` | Unique slug identifying the template. |
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
### Definition
`POST /wp/v2/templates//autosaves`
## Retrieve a Template Revision
### Definition & Example Request
`GET /wp/v2/templates//autosaves/`
Query this endpoint to retrieve a specific template revision record.
`$ curl https://example.com/wp-json/wp/v2/templates//autosaves/`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `id` | The ID for the autosave. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Template_Part Revisions
Source: https://developer.wordpress.org/rest-api/reference/wp_template_part-revisions/
## Schema
The schema defines all the fields that exist within a template\_part revision record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `author` | The ID for the author of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
|---|---|
| `date` | The date the revision was published, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit`, `embed` |
| `date_gmt` | The date the revision was published, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `guid` | GUID for the revision, as it exists in the database. JSON data type: string Context: `view`, `edit` |
| `id` | Unique identifier for the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the revision was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `modified_gmt` | The date the revision was last modified, as GMT. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Context: `view`, `edit` |
| `parent` | The ID for the parent of the revision. JSON data type: integer Context: `view`, `edit`, `embed` |
| `slug` | An alphanumeric identifier for the revision unique to its type. JSON data type: string Context: `view`, `edit`, `embed` |
| `title` | Title of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `content` | Content of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
## List Template\_Part Revisions
Query this endpoint to retrieve a collection of template\_part revisions. The response you receive can be controlled and filtered using the URL query parameters below.
### Definition
`GET /wp/v2/template-parts//revisions`
### Example Request
`$ curl https://example.com/wp-json/wp/v2/template-parts//revisions`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
| `page` | Current page of the collection. Default: `1` |
| `per_page` | Maximum number of items to be returned in result set. |
| `search` | Limit results to those matching a string. |
| `exclude` | Ensure result set excludes specific IDs. |
| `include` | Limit result set to specific IDs. |
| `offset` | Offset the result set by a specific number of items. |
| `order` | Order sort attribute ascending or descending. Default: `desc` One of: `asc`, `desc` |
| `orderby` | Sort collection by object attribute. Default: `date` One of: `date`, `id`, `include`, `relevance`, `slug`, `include_slugs`, `title` |
## Retrieve a Template\_Part Revision
### Definition & Example Request
`GET /wp/v2/template-parts//revisions/`
Query this endpoint to retrieve a specific template\_part revision record.
`$ curl https://example.com/wp-json/wp/v2/template-parts//revisions/`
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Delete a Template\_Part Revision
### Arguments
| `parent` | The ID for the parent of the revision. |
|---|---|
| `id` | Unique identifier for the revision. |
| `force` | Required to be true, as revisions do not support trashing. |
### Definition
`DELETE /wp/v2/template-parts//revisions/`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/template-parts//revisions/`
## Retrieve a Template\_Part Revision
### Definition & Example Request
`GET /wp/v2/template-parts//autosaves`
Query this endpoint to retrieve a specific template\_part revision record.
`$ curl https://example.com/wp-json/wp/v2/template-parts//autosaves`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Create a Template\_Part Revision
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `slug` | Unique slug identifying the template. |
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
| `area` | Where the template part is intended for use (header, footer, etc.) |
### Definition
`POST /wp/v2/template-parts//autosaves`
## Retrieve a Template\_Part Revision
### Definition & Example Request
`GET /wp/v2/template-parts//autosaves/`
Query this endpoint to retrieve a specific template\_part revision record.
`$ curl https://example.com/wp-json/wp/v2/template-parts//autosaves/`
### Arguments
| `parent` | The ID for the parent of the autosave. |
|---|---|
| `id` | The ID for the autosave. |
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
---
# Template_Parts
Source: https://developer.wordpress.org/rest-api/reference/wp_template_parts/
## Schema
The schema defines all the fields that exist within a template\_part record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | ID of template. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `slug` | Unique slug identifying the template. JSON data type: string Context: `embed`, `view`, `edit` |
| `theme` | Theme identifier for the template. JSON data type: string Context: `embed`, `view`, `edit` |
| `type` | Type of template. JSON data type: string Context: `embed`, `view`, `edit` |
| `source` | Source of template JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `origin` | Source of a customized template JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `content` | Content of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `title` | Title of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `description` | Description of template. JSON data type: string Context: `embed`, `view`, `edit` |
| `status` | Status of template. JSON data type: string Context: `embed`, `view`, `edit` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `wp_id` | Post ID. JSON data type: integer Read only Context: `embed`, `view`, `edit` |
| `has_theme_file` | Theme file exists. JSON data type: bool Read only Context: `embed`, `view`, `edit` |
| `author` | The ID for the author of the template. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the template was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `area` | Where the template part is intended for use (header, footer, etc.) JSON data type: string Context: `embed`, `view`, `edit` |
## Retrieve a Template\_Part
### Definition & Example Request
`GET /wp/v2/template-parts`
Query this endpoint to retrieve a specific template\_part record.
`$ curl https://example.com/wp-json/wp/v2/template-parts`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `wp_id` | Limit to the specified post id. |
| `area` | Limit to the specified template part area. |
| `post_type` | Post type to get the templates for. |
## Create a Template\_Part
### Arguments
| `slug` | Unique slug identifying the template. Required: 1 |
|---|---|
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. Default: `publish` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
| `area` | Where the template part is intended for use (header, footer, etc.) |
### Definition
`POST /wp/v2/template-parts`
## Retrieve a Template\_Part
### Definition & Example Request
`GET /wp/v2/template-parts/?)[\/\w%-]+)`
Query this endpoint to retrieve a specific template\_part record.
`$ curl https://example.com/wp-json/wp/v2/template-parts/?)[\/\w%-]+)`
### Arguments
| `id` | The id of a template |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Update a Template\_Part
### Arguments
| `id` | The id of a template |
|---|---|
| `slug` | Unique slug identifying the template. |
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
| `area` | Where the template part is intended for use (header, footer, etc.) |
### Definition
`POST /wp/v2/template-parts/?)[\/\w%-]+)`
### Example Request
``
## Delete a Template\_Part
### Arguments
| `id` | The id of a template |
|---|---|
| `force` | Whether to bypass Trash and force deletion. |
### Definition
`DELETE /wp/v2/template-parts/?)[\/\w%-]+)`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/template-parts/?)[\/\w%-]+)`
---
# Templates
Source: https://developer.wordpress.org/rest-api/reference/wp_templates/
## Schema
The schema defines all the fields that exist within a template record. Any response from these endpoints can be expected to contain the fields below unless the `\_filter` query parameter is used or the schema field only appears in a specific context.
| `id` | ID of template. JSON data type: string Read only Context: `embed`, `view`, `edit` |
|---|---|
| `slug` | Unique slug identifying the template. JSON data type: string Context: `embed`, `view`, `edit` |
| `theme` | Theme identifier for the template. JSON data type: string Context: `embed`, `view`, `edit` |
| `type` | Type of template. JSON data type: string Context: `embed`, `view`, `edit` |
| `source` | Source of template JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `origin` | Source of a customized template JSON data type: string Read only Context: `embed`, `view`, `edit` |
| `content` | Content of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `title` | Title of template. JSON data type: object or string Context: `embed`, `view`, `edit` |
| `description` | Description of template. JSON data type: string Context: `embed`, `view`, `edit` |
| `status` | Status of template. JSON data type: string Context: `embed`, `view`, `edit` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `wp_id` | Post ID. JSON data type: integer Read only Context: `embed`, `view`, `edit` |
| `has_theme_file` | Theme file exists. JSON data type: bool Read only Context: `embed`, `view`, `edit` |
| `author` | The ID for the author of the template. JSON data type: integer Context: `view`, `edit`, `embed` |
| `modified` | The date the template was last modified, in the site's timezone. JSON data type: string, Format: datetime ([details](https://core.trac.wordpress.org/ticket/41032)) Read only Context: `view`, `edit` |
| `is_custom` | Whether a template is a custom template. JSON data type: bool Read only Context: `embed`, `view`, `edit` |
## Retrieve a Template
### Definition & Example Request
`GET /wp/v2/templates`
Query this endpoint to retrieve a specific template record.
`$ curl https://example.com/wp-json/wp/v2/templates`
### Arguments
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
|---|---|
| `wp_id` | Limit to the specified post id. |
| `area` | Limit to the specified template part area. |
| `post_type` | Post type to get the templates for. |
## Create a Template
### Arguments
| `slug` | Unique slug identifying the template. Required: 1 |
|---|---|
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. Default: `publish` One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
### Definition
`POST /wp/v2/templates`
## Retrieve a Template
### Definition & Example Request
`GET /wp/v2/templates/?)[\/\w%-]+)`
Query this endpoint to retrieve a specific template record.
`$ curl https://example.com/wp-json/wp/v2/templates/?)[\/\w%-]+)`
### Arguments
| `id` | The id of a template |
|---|---|
| `context` | Scope under which the request is made; determines fields present in response. Default: `view` One of: `view`, `embed`, `edit` |
## Update a Template
### Arguments
| `id` | The id of a template |
|---|---|
| `slug` | Unique slug identifying the template. |
| `theme` | Theme identifier for the template. |
| `type` | Type of template. |
| `content` | Content of template. |
| `title` | Title of template. |
| `description` | Description of template. |
| `status` | Status of template. One of: `publish`, `future`, `draft`, `pending`, `private` |
| `author` | The ID for the author of the template. |
### Definition
`POST /wp/v2/templates/?)[\/\w%-]+)`
### Example Request
``
## Delete a Template
### Arguments
| `id` | The id of a template |
|---|---|
| `force` | Whether to bypass Trash and force deletion. |
### Definition
`DELETE /wp/v2/templates/?)[\/\w%-]+)`
### Example Request
`$ curl -X DELETE https://example.com/wp-json/wp/v2/templates/?)[\/\w%-]+)`
---
# REST API Handbook
Source: https://developer.wordpress.org/rest-api/
The WordPress REST API provides an interface for applications to interact with your WordPress site by sending and receiving data as [JSON](https://en.wikipedia.org/wiki/JSON) (JavaScript Object Notation) objects. It is the foundation of the [WordPress Block Editor](https://developer.wordpress.org/block-editor/), and can likewise enable your theme, plugin or custom application to present new, powerful interfaces for managing and publishing your site content.
Using the WordPress REST API you can create a plugin to provide an entirely new admin experience for WordPress, build a brand new interactive front-end experience, or bring your WordPress content into completely separate applications.
The REST API is a developer-oriented feature of WordPress. It provides data access to the content of your site, and implements the same authentication restrictions — content that is public on your site is generally publicly accessible via the REST API, while private content, password-protected content, internal users, custom post types, and metadata is only available with authentication or if you specifically set it to be so. If you are not a developer, the most important thing to understand about the API is that it enables the block editor and modern plugin interfaces without compromising the security or privacy of your site.
## What Is A REST API?
An API is an Application Programming Interface. REST, standing for “REpresentational State Transfer,” is a set of concepts for modeling and accessing your application’s data as interrelated objects and collections. The WordPress REST API provides REST endpoints (URLs) representing the posts, pages, taxonomies, and other built-in WordPress data types. Your application can send and receive JSON data to these endpoints to query, modify and create content on your site. JSON is an open standard data format that is lightweight and human-readable, and looks like Objects do in JavaScript. When you request content from or send content to the API, the response will also be returned in JSON. Because JSON is widely supported in many programming languages, developers can build WordPress applications in client-side JavaScript (like the block editor), as mobile apps, or as desktop or command line tools.
The REST API is just one of many APIs provided by WordPress. You can find the [documentation on these additional APIs here](https://codex.wordpress.org/WordPress_APIs).
## Using the WordPress REST API
WordPress already provides a rich set of tools and interfaces for building sites, and you should not feel pressured to use the REST API if your site is already working the way you expect. You do not need to use the REST API to build a WordPress theme or plugin.
However, if you do wish to write your theme, plugin, or external application as a client-side JavaScript application, or a standalone program in a language other than PHP, then your application will need a structured way to access content within your WordPress site. Any programming language which can make HTTP requests and interpret JSON can use the REST API to interact with WordPress, from PHP, Node.js, Go, and Java, to Swift, Kotlin, and beyond.
Even if you’re using vanilla JavaScript or jQuery within a theme or plugin the REST API provides a more predictable and structured way to interact with your site’s content than [`admin-ajax`](https://codex.wordpress.org/AJAX_in_Plugins), enabling you to spend less time accessing the data you need and more time creating better user experiences.
If you want a structured, extensible, and simple way to get data in and out of WordPress, you probably want to use the REST API.
For all of its simplicity the REST API can feel quite complex at first, so in this handbook we will attempt to break it down into smaller components to explain each part of the full puzzle.
## Next Steps
Familiarize yourself with the [key technical concepts](https://developer.wordpress.org/rest-api/key-concepts/) behind how the REST API functions.
Learn more about how to interact with API resources and query for specific data in the [Using the REST API](https://developer.wordpress.org/rest-api/using-the-rest-api/) section.
Once you’re comfortable with the default workings of the default routes and methods, discover how to add new data to the API or enhance and manipulate existing response objects in the [Extending the REST API](https://developer.wordpress.org/rest-api/extending-the-rest-api/) section.
For a comprehensive overview of the resources and routes available by default, review the [API reference](https://developer.wordpress.org/rest-api/reference/).
---
# Authentication
Source: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/
## Cookie Authentication
Cookie authentication is the standard authentication method included with WordPress. When you log in to your dashboard, this sets up the cookies correctly for you, so plugin and theme developers need only to have a logged-in user. However, the REST API includes a technique called [nonces](https://developer.wordpress.org/apis/security/nonces/) to avoid [CSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery) issues. This prevents other sites from forcing you to perform actions without explicitly intending to do so. This requires slightly special handling for the API. For developers using the built-in Javascript API, this is handled automatically for you. This is the recommended way to use the API for plugins and themes. Custom data models can extend `wp.api.models.Base` to ensure this is sent correctly for any custom requests. For developers making manual Ajax requests, the nonce will need to be passed with each request. The API uses nonces with the action set to `wp_rest`. These can then be passed to the API via the `_wpnonce` data parameter (either POST data or in the query for GET requests), or via the `X-WP-Nonce` header. If no nonce is provided the API will set the current user to 0, turning the request into an **unauthenticated request**, even if you’re logged into WordPress. Note: Until recently, most software had spotty support for `DELETE` requests. For instance, PHP doesn’t transform the request body of a `DELETE` request into a super global. As such, supplying the nonce as a header is the most reliable approach. It is important to keep in mind that this authentication method relies on WordPress cookies. As a result this method is only applicable when the REST API is used inside of WordPress and the current user is logged in. In addition, the current user must have the appropriate capability to perform the action being performed. As an example, this is how the built-in Javascript client creates the nonce: ```php
esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
```
This is then used in the base model: ```js
options.beforeSend = function(xhr) {
xhr.setRequestHeader('X-WP-Nonce', wpApiSettings.nonce);
if (beforeSend) {
return beforeSend.apply(this, arguments);
}
};
```
Here is an example of editing the title of a post, using jQuery AJAX: ```js
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
```
Note that you do not need to verify that the nonce is valid inside your custom end point. This is automatically done for you in `rest_cookie_check_errors()`. ## Basic Authentication with Application Passwords
As of 5.6, WordPress has shipped with [Application Passwords](https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/), which can be generated from an Edit User page (wp-admin -> Users -> Edit User). The credentials can be passed along to REST API requests served over https:// using [Basic Auth](https://ec.haxx.se/http/http-auth) / [RFC 7617](https://tools.ietf.org/html/rfc7617) — [here’s the documentation for how to use it with cURL](https://ec.haxx.se/http/http-auth). For a simple command-line script example, just swap out USERNAME, PASSWORD, and HOSTNAME in this with their respective values: ```
curl --user "USERNAME:PASSWORD" https://HOSTNAME/wp-json/wp/v2/users?context=edit
```
## Authentication Plugins
Plugins may be added to support alternative modes of authentication that will work from remote applications. Some example plugins are [OAuth 1.0a Server](https://wordpress.org/plugins/rest-api-oauth1/) and [JSON Web Tokens](https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/).
There’s also a [Basic Authentication](https://github.com/WP-API/Basic-Auth) plugin. Note that this plugin requires sending your username and password with every request, and should only be used for development and testing i.e. not in a production environment. Using Application Passwords (see above) is preferred.
---
# Routes and Endpoints
Source: https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
## Overview
The REST API provides us a way to match URIs to various resources in our WordPress install. By default, if you have pretty permalinks enabled, the WordPress REST API “lives” at `/wp-json/`. At our WordPress site `https://ourawesomesite.com`, we can access the REST API's index by making a`GET`request to`https://ourawesomesite.com/wp-json/`. The index provides information regarding what routes are available for that particular WordPress install, along with what HTTP methods are supported and what endpoints are registered.
## Routes vs Endpoints
Endpoints are functions available through the API. This can be things like retrieving the API index, updating a post, or deleting a comment. Endpoints perform a specific function, taking some number of parameters and return data to the client.
A route is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb.
For example, with the URL `http://example.com/wp-json/wp/v2/posts/123`:
- The “route” is `wp/v2/posts/123` – The route doesn’t include `wp-json` because `wp-json` is the base path for the API itself.
- This route has 3 endpoints:
- `GET` triggers a `get_item` method, returning the post data to the client.
- `PUT` triggers an `update_item` method, taking the data to update, and returning the updated post data.
- `DELETE` triggers a `delete_item` method, returning the now-deleted post data to the client.
On sites without pretty permalinks, the route is instead added to the URL as the `rest_route` parameter. For the above example, the full URL would then be `http://example.com/?rest_route=/wp/v2/posts/123`
## Creating Endpoints
If we wanted to create an endpoint that would return the phrase “Hello World, this is the WordPress REST API” when it receives a GET request, we would first need to register the route for that endpoint. To register routes you should use the `register_rest_route()` function. It needs to be called on the `rest_api_init` action hook. `register_rest_route()` handles all of the mapping for routes to endpoints. Let’s try to create a “Hello World, this is the WordPress REST API” route.
```php
/**
* This is our callback function that embeds our phrase in a WP_REST_Response
*/
function prefix_get_endpoint_phrase() {
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response( 'Hello World, this is the WordPress REST API' );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_example_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'hello-world/v1', '/phrase', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_endpoint_phrase',
) );
}
add_action( 'rest_api_init', 'prefix_register_example_routes' );
```
The first argument passed into `register_rest_route()` is the namespace, which provides us a way to group our routes. The second argument passed in is the resource path, or resource base. For our example, the resource we are retrieving is the “Hello World, this is the WordPress REST API” phrase. The third argument is an array of options. We specify what methods the endpoint can use and what callback should happen when the endpoint is matched (more things can be done but these are the fundamentals).
The third argument also allows us to provide a permissions callback, which can restrict access for the endpoint to only certain users. The third argument also offers a way to register arguments for the endpoint so that requests can modify the response of our endpoint. We will get into those concepts in the endpoints section of this guide.
When we go to `https://ourawesomesite.com/wp-json/hello-world/v1/phrase` we can now see our REST API greeting us kindly. Let’s take a look at routes a bit more in depth.
## Routes
Routes in the REST API are represented by URIs. The route itself is what is tacked onto the end of `https://ourawesomesite.com/wp-json`. The index route for the API is`/`which is why`https://ourawesomesite.com/wp-json/` returns all of the available information for the API. All routes should be built onto this route, the `wp-json` portion can be changed, but in general, it is advised to keep it the same.
We want to make sure that our routes are unique. For instance we could have a route for books like this: `/books`. Our books route would now live at `https://ourawesomesite.com/wp-json/books`. However, this is not a good practice as we would end up polluting potential routes for the API. What if another plugin we wanted to register a books route as well? We would be in big trouble in that case, as the two routes would conflict with each other and only one could be used. The fourth parameter to`[register\_rest\_route()](https://developer.wordpress.org/reference/functions/register_rest_route/) ` is a boolean for whether the route should override an existing route.
The override parameter does not really solve our problem either, as both routes could override or we would want to use both routes for different things. This is where using namespaces for our routes comes in.
### Namespaces
It is extremely important to add namespaces to your routes. The “core” endpoints use the `wp/v2` namespace.
**Do not place anything into the `wp` namespace unless you are making endpoints with the intention of merging them into core.**
There are some key things to take notice of in the core endpoint namespace. The first part of the namespace is `wp`, which represents the vendor name; WordPress. For our plugins we will want to come up with unique names for what we call the vendor portion of the namespace. In the example above we used `hello-world`.
Following the vendor portion is the version portion of the namespace. The “core” endpoints utilize `v2` to represent version 2 of the WordPress REST API. If you are writing a plugin, you can maintain backwards compatibility of your REST API endpoints by simply creating new endpoints and bumping up the version number you provide. This way both the original `v1` and `v2` endpoints can be accessed.
The part of the route that follows the namespace is the resource path.
### Resource Paths
The resource path should signify what resource the endpoint is associated with. In the example we used above, we used the word `phrase` to signify that the resource we are interacting with is a phrase. To avoid any collisions, each resource path we register should also be unique within a namespace. Resource paths should be used to define different resource routes within a given namespace.
Let’s say we have a plugin that handles some basic eCommerce functionality. We will have two main resource types orders, and products. Orders are a request for product(s) but they are not the product themselves. The same concept applies to products. Although these resources are related they are not the same thing and each should live in a separate resource paths. Our routes will end up looking something like this for our eCommerce plugin: `/my-shop/v1/orders` and `/my-shop/v1/products`.
Using routes like this, we would want each to return a collection of orders or products. What if we wanted to grab a specific product by ID, we would need to use path variables in our routes.
### Path Variables
Path variables enable us to add dynamic routes. To expand on our eCommerce routes, we could register a route to grab individual products.
```php
/**
* This is our callback function to return our products.
*
* @param WP_REST_Request $request This function accepts a rest request to process data.
*/
function prefix_get_products( $request ) {
// In practice this function would fetch the desired data. Here we are just making stuff up.
$products = array(
'1' => 'I am product 1',
'2' => 'I am product 2',
'3' => 'I am product 3',
);
return rest_ensure_response( $products );
}
/**
* This is our callback function to return a single product.
*
* @param WP_REST_Request $request This function accepts a rest request to process data.
*/
function prefix_get_product( $request ) {
// In practice this function would fetch the desired data. Here we are just making stuff up.
$products = array(
'1' => 'I am product 1',
'2' => 'I am product 2',
'3' => 'I am product 3',
);
// Here we are grabbing the 'id' path variable from the $request object. WP_REST_Request implements ArrayAccess, which allows us to grab properties as though it is an array.
$id = (string) $request['id'];
if ( isset( $products[ $id ] ) ) {
// Grab the product.
$product = $products[ $id ];
// Return the product as a response.
return rest_ensure_response( $product );
} else {
// Return a WP_Error because the request product was not found. In this case we return a 404 because the main resource was not found.
return new WP_Error( 'rest_product_invalid', esc_html__( 'The product does not exist.', 'my-text-domain' ), array( 'status' => 404 ) );
}
// If the code somehow executes to here something bad happened return a 500.
return new WP_Error( 'rest_api_sad', esc_html__( 'Something went horribly wrong.', 'my-text-domain' ), array( 'status' => 500 ) );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_product_routes() {
// Here we are registering our route for a collection of products.
register_rest_route( 'my-shop/v1', '/products', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_products',
) );
// Here we are registering our route for single products. The (?P[\d]+) is our path variable for the ID, which, in this example, can only be some form of positive number.
register_rest_route( 'my-shop/v1', '/products/(?P[\d]+)', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_product',
) );
}
add_action( 'rest_api_init', 'prefix_register_product_routes' );
```
The above example covers a lot. The important part to note is that in the second route we register, we add on a path variable `/(?P[\d]+)` to our resource path `/products`. The path variable is a regular expression. In this case it uses `[\d]+` to signify that should be any numerical character at least once. If you are using numeric IDs for your resources, then this is a great example of how to use a path variable. When using path variables, we now have to be careful around what can be matched as it is user input.
The regex luckily will filter out anything that is not numerical. However, what if the product for the requested ID doesn’t exist. We need to do error handling. You can see the basic way we are handling errors in the code example above. When you return a `WP_Error` in your endpoint callbacks the API server will automatically handle serving the error to the client.
Although this section is about routes, we have covered quite a bit about endpoints. Endpoints and routes are interrelated, but they definitely have distinctions.
## Endpoints
Endpoints are the destination that a route needs to map to. For any given route, you could have a number of different endpoints registered to it. We will expand on our fictitious eCommerce plugin, to better show the distinction between routes and endpoints. We are going to create two endpoints that exist at the `/wp-json/my-shop/v1/products/` route. One endpoint uses the HTTP verb `GET` to get products, and the other endpoint uses the HTTP verb `POST` to create a new product.
```php
/**
* This is our callback function to return our products.
*
* @param WP_REST_Request $request This function accepts a rest request to process data.
*/
function prefix_get_products( $request ) {
// In practice this function would fetch the desired data. Here we are just making stuff up.
$products = array(
'1' => 'I am product 1',
'2' => 'I am product 2',
'3' => 'I am product 3',
);
return rest_ensure_response( $products );
}
/**
* This is our callback function to return a single product.
*
* @param WP_REST_Request $request This function accepts a rest request to process data.
*/
function prefix_create_product( $request ) {
// In practice this function would create a product. Here we are just making stuff up.
return rest_ensure_response( 'Product has been created' );
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_product_routes() {
// Here we are registering our route for a collection of products and creation of products.
register_rest_route( 'my-shop/v1', '/products', array(
array(
// By using this constant we ensure that when the WP_REST_Server changes, our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_products',
),
array(
// By using this constant we ensure that when the WP_REST_Server changes, our create endpoints will work as intended.
'methods' => WP_REST_Server::CREATABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_create_product',
),
) );
}
add_action( 'rest_api_init', 'prefix_register_product_routes' );
```
Depending on what HTTP Method we use for the route `/wp-json/my-shop/v1/products`, we are matched to a different endpoint and a different callback is fired. When we use `POST` we trigger the `prefix_create_product()` callback, and when we use `GET` we trigger the `prefix_get_products()` callback.
There are a number of different HTTP methods and the REST API can make use of any of them.
### HTTP Methods
HTTP methods are sometimes referred to as HTTP verbs. They are simply just different ways to communicate via HTTP. The main ones used by the WordPress REST API are:
- `GET` should be used for retrieving data from the API.
- `POST` should be used for creating new resources (i.e users, posts, taxonomies).
- `PUT` should be used for updating resources.
- `DELETE` should be used for deleting resources.
- `OPTIONS` should be used to provide context about our resources.
It is important to note that these methods are not supported by every client, as they were introduced in HTTP 1.1. Luckily, the API provides a workaround for these unfortunate cases. If you want to delete a resource but can’t send a `DELETE` request, then you can use the `_method` parameter or the `X-HTTP-Method-Override` header in your request. How this works is you will send a `POST` request to `https://ourawesomesite.com/wp-json/my-shop/v1/products/1?\_method=DELETE`. Now you will have deleted product number 1, even though your client could not send the proper HTTP method in the request, or maybe there was a firewall in place that blocks out DELETE requests.
The HTTP method, in combination with the route and callbacks, are what make up the core of an endpoint.
### Callbacks
There are currently only two types of callbacks for endpoints supported by the REST API; `callback` and `permission_callback`. The main callback should handle the interaction with the resource. The permissions callback should handle what users have access to the endpoint. You can add additional callbacks by adding additional information when registering an endpoint. You can then hook into `rest_pre_dispatch`, `rest_dispatch_request`, or `rest_post_dispatch` hooks to fire your new custom callbacks.
#### Endpoint Callback
The main callback for a delete endpoint should only delete the resource and return a copy of it in the response. The main callback for a creation endpoint should only create the resource and return a response matching the newly created data. An update callback should only modify resources that actually exist. A reading callback should only retrieve data that already exists. It is important to take into account the concept of idempotence.
Idempotence, in the context of a REST API, means that if you make the same request to an endpoint the server will process the request the same way. Imagine if our read endpoint was not idempotent. Whenever we made a request to it the state of our server would be modified by the request, even though we were only trying to get data. This could be catastrophic. Any time someone fetched data from your server something would change internally. It is important to make sure that read, update, and delete endpoints do not have nasty side effects and just stick to what they are intended to do.
In a REST API, the concept of idempotence is tied to HTTP methods instead of endpoint callbacks. Any callback using `GET`, `HEAD`, `TRACE`, `OPTIONS`, `PUT`, or `DELETE`, should not produce any side effects. `POST` requests are not idempotent, and are typically used for creating resources. If you created an idempotent creation method then you would only ever create one resource because when you make the same request there would be no more side effects to the server. For creating, if you make the same request over and over the server should generate new resources each time.
To restrict usage of endpoints we need to register a permissions callback.
#### Permissions Callback
Permissions callbacks are extremely important for security with the WordPress REST API. If you have any private data that should not be displayed publicly, then you need to have permissions callbacks registered for your endpoints. Below is an example of how to register permissions callbacks.
```php
/**
* This is our callback function that embeds our resource in a WP_REST_Response
*/
function prefix_get_private_data() {
// rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
return rest_ensure_response( 'This is private data.' );
}
/**
* This is our callback function that embeds our resource in a WP_REST_Response
*/
function prefix_get_private_data_permissions_check() {
// Restrict endpoint to only users who have the edit_posts capability.
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'OMG you can not view private data.', 'my-text-domain' ), array( 'status' => 401 ) );
}
// This is a black-listing approach. You could alternatively do this via white-listing, by returning false here and changing the permissions check.
return true;
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_example_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'my-plugin/v1', '/private-data', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_private_data',
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'permission_callback' => 'prefix_get_private_data_permissions_check',
) );
}
add_action( 'rest_api_init', 'prefix_register_example_routes' );
```
If you try out this endpoint without any Authentication enabled then you will also be returned the error response, preventing you from seeing the data. Authentication is a huge topic and eventually a portion of this chapter will be created to show you how to create your own authentication processes.
### Arguments
When making requests to an endpoint you might need to specify extra parameters to change the response. These extra parameters can be added while registering endpoints. Let’s look at an example of how to use arguments with an endpoint.
```php
/**
* This is our callback function that embeds our resource in a WP_REST_Response
*/
function prefix_get_colors( $request ) {
// In practice this function would fetch the desired data. Here we are just making stuff up.
$colors = array(
'blue',
'blue',
'red',
'red',
'green',
'green',
);
if ( isset( $request['filter'] ) ) {
$filtered_colors = array();
foreach ( $colors as $color ) {
if ( $request['filter'] === $color ) {
$filtered_colors[] = $color;
}
}
return rest_ensure_response( $filtered_colors );
}
return rest_ensure_response( $colors );
}
/**
* We can use this function to contain our arguments for the example product endpoint.
*/
function prefix_get_color_arguments() {
$args = array();
// Here we are registering the schema for the filter argument.
$args['filter'] = array(
// description should be a human readable description of the argument.
'description' => esc_html__( 'The filter parameter is used to filter the collection of colors', 'my-text-domain' ),
// type specifies the type of data that the argument should be.
'type' => 'string',
// enum specified what values filter can take on.
'enum' => array( 'red', 'green', 'blue' ),
);
return $args;
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_example_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'my-colors/v1', '/colors', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_colors',
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'args' => prefix_get_color_arguments(),
) );
}
add_action( 'rest_api_init', 'prefix_register_example_routes' );
```
We have now specified a `filter` argument for this example. We can specify the argument as a query parameter when we request the endpoint. If we make a `GET` request to `https://ourawesomesitem.com/my-colors/v1/colors?filter=blue`, we will be returned only the blue colors in our collection. You could also pass these as body parameters in the request body, instead of in the query string. To understand the distinction between query parameters and body parameters you should read about the HTTP spec. Query parameters live in the query string tacked onto the URL and body parameters are directly embedded in the body of an HTTP request.
We have created an argument for our endpoint, but how do we verify that the argument is a string and tell whether it matches the value red, green, or blue. To do this we need to specify a validation callback for our argument.
#### Validation
Validation and sanitization are extremely important for security in the API. The validate callback (in WP 4.6+), fires before the sanitize callback. You should use the `validate_callback` for your arguments to verify whether the input you are receiving is valid. The `sanitize_callback` should be used to transform the argument input or clean out unwanted parts out of the argument, before the argument is processed by the main callback.
In the example above, we need to verify that the `filter` parameter is a string, and it matches the value red, green, or blue. Let’s look at what the code looks like after adding in a `validate_callback`.
```php
/**
* This is our callback function that embeds our resource in a WP_REST_Response
*/
function prefix_get_colors( $request ) {
// In practice this function would fetch more practical data. Here we are just making stuff up.
$colors = array(
'blue',
'blue',
'red',
'red',
'green',
'green',
);
if ( isset( $request['filter'] ) ) {
$filtered_colors = array();
foreach ( $colors as $color ) {
if ( $request['filter'] === $color ) {
$filtered_colors[] = $color;
}
}
return rest_ensure_response( $filtered_colors );
}
return rest_ensure_response( $colors );
}
/**
* Validate a request argument based on details registered to the route.
*
* @param mixed $value Value of the 'filter' argument.
* @param WP_REST_Request $request The current request object.
* @param string $param Key of the parameter. In this case it is 'filter'.
* @return WP_Error|boolean
*/
function prefix_filter_arg_validate_callback( $value, $request, $param ) {
// If the 'filter' argument is not a string return an error.
if ( ! is_string( $value ) ) {
return new WP_Error( 'rest_invalid_param', esc_html__( 'The filter argument must be a string.', 'my-text-domain' ), array( 'status' => 400 ) );
}
// Get the registered attributes for this endpoint request.
$attributes = $request->get_attributes();
// Grab the filter param schema.
$args = $attributes['args'][ $param ];
// If the filter param is not a value in our enum then we should return an error as well.
if ( ! in_array( $value, $args['enum'], true ) ) {
return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not one of %s' ), $param, implode( ', ', $args['enum'] ) ), array( 'status' => 400 ) );
}
}
/**
* We can use this function to contain our arguments for the example product endpoint.
*/
function prefix_get_color_arguments() {
$args = array();
// Here we are registering the schema for the filter argument.
$args['filter'] = array(
// description should be a human readable description of the argument.
'description' => esc_html__( 'The filter parameter is used to filter the collection of colors', 'my-text-domain' ),
// type specifies the type of data that the argument should be.
'type' => 'string',
// enum specified what values filter can take on.
'enum' => array( 'red', 'green', 'blue' ),
// Here we register the validation callback for the filter argument.
'validate_callback' => 'prefix_filter_arg_validate_callback',
);
return $args;
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_example_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'my-colors/v1', '/colors', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_colors',
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'args' => prefix_get_color_arguments(),
) );
}
add_action( 'rest_api_init', 'prefix_register_example_routes' );
```
#### Sanitizing
In the above example, we do not need to use a `sanitize_callback`, because we are restricting input to only values in our enum. If we did not have strict validation and accepted any string as a parameter, we would definitely need to register a `sanitize_callback`. What if we wanted to update a content field and the user entered something like `alert('ZOMG Hacking you');`. The field value could potentially be an executable script. To strip out unwanted data or to transform data into a desired format we need to register a `sanitize_callback` for our arguments. Here is an example of how to use WordPress’s `sanitize_text_field()` for a sanitize callback:
```php
/**
* This is our callback function that embeds our resource in a WP_REST_Response.
*
* The parameter is already sanitized by this point so we can use it without any worries.
*/
function prefix_get_item( $request ) {
if ( isset( $request['data'] ) ) {
return rest_ensure_response( $request['data'] );
}
return new WP_Error( 'rest_invalid', esc_html__( 'The data parameter is required.', 'my-text-domain' ), array( 'status' => 400 ) );
}
/**
* Validate a request argument based on details registered to the route.
*
* @param mixed $value Value of the 'filter' argument.
* @param WP_REST_Request $request The current request object.
* @param string $param Key of the parameter. In this case it is 'filter'.
* @return WP_Error|boolean
*/
function prefix_data_arg_validate_callback( $value, $request, $param ) {
// If the 'data' argument is not a string return an error.
if ( ! is_string( $value ) ) {
return new WP_Error( 'rest_invalid_param', esc_html__( 'The filter argument must be a string.', 'my-text-domain' ), array( 'status' => 400 ) );
}
}
/**
* Sanitize a request argument based on details registered to the route.
*
* @param mixed $value Value of the 'filter' argument.
* @param WP_REST_Request $request The current request object.
* @param string $param Key of the parameter. In this case it is 'filter'.
* @return WP_Error|boolean
*/
function prefix_data_arg_sanitize_callback( $value, $request, $param ) {
// It is as simple as returning the sanitized value.
return sanitize_text_field( $value );
}
/**
* We can use this function to contain our arguments for the example product endpoint.
*/
function prefix_get_data_arguments() {
$args = array();
// Here we are registering the schema for the filter argument.
$args['data'] = array(
// description should be a human readable description of the argument.
'description' => esc_html__( 'The data parameter is used to be sanitized and returned in the response.', 'my-text-domain' ),
// type specifies the type of data that the argument should be.
'type' => 'string',
// Set the argument to be required for the endpoint.
'required' => true,
// We are registering a basic validation callback for the data argument.
'validate_callback' => 'prefix_data_arg_validate_callback',
// Here we register the validation callback for the filter argument.
'sanitize_callback' => 'prefix_data_arg_sanitize_callback',
);
return $args;
}
/**
* This function is where we register our routes for our example endpoint.
*/
function prefix_register_example_routes() {
// register_rest_route() handles more arguments but we are going to stick to the basics for now.
register_rest_route( 'my-plugin/v1', '/sanitized-data', array(
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
'methods' => WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => 'prefix_get_item',
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'args' => prefix_get_data_arguments(),
) );
}
add_action( 'rest_api_init', 'prefix_register_example_routes' );
```
## Summary
We have covered the basics of registering endpoints for the WordPress REST API. Routes are the URIs that our endpoints live at. Endpoints are a collection of callbacks, methods, args, and other options. Each endpoint is mapped to a route when using `register_rest_route()`. An endpoint by default can support various HTTP methods, a main callback, a permissions callback, and registered arguments. We can register endpoints to cover any of our use cases for interacting with WordPress. The endpoints serve as the core interaction point with the REST API, but there are many other topics to explore and understand, to fully utilize this powerful API.
---
# Controller Classes
Source: https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/
## Overview
To register a new REST route, you must specify a number of callback functions to control endpoint behavior such as how a request is fulfilled, how permissions checks are applied, and how the schema for your resource gets generated. While it is possible to declare all of these methods in an ordinary PHP file without any wrapping namespace or class, all functions declared in that manner coexist in the same global scope. If you decide to use a common function name for your endpoint logic like `get_items()` and another plugin (or another endpoint in your own plugin) also registers a function with that same name, PHP will fail with a fatal error because the function `get_items()` is being declared twice. You can avoid this issue by naming your callback functions using a unique prefix such as `myplugin_myendpoint_` to avoid any potential conflics: ```php
function myplugin_myendpoint_register_routes() { /* ... */ }
function myplugin_myendpoint_get_item() { /* ... */ }
function myplugin_myendpoint_get_item_schema() { /* ... */ }
// etcetera
add_action( 'rest_api_init', 'myplugin_myendpoint_register_routes' );
```
You may already be familiar with this approach because it is commonly used within theme `functions.php` files. However these prefixes are unnecessarily verbose, and several better options exist to group and encapsulate your endpoint’s logic in a more maintainable way. WordPress currently requires PHP 5.6 or greater. PHP 5.6 supports [namespaces](https://www.php.net/manual/en/language.namespaces.rationale.php), which provide an easy way to encapsulate your endpoint’s functionality. By declaring a `namespace` at the top of your endpoint’s PHP file, all methods within that namespace will be declared within that namespace and will no longer conflict with global functions. You may then use shorter, more-readable names for your endpoint callbacks. ```php
namespace MyPlugin\API\MyEndpoint;
function register_routes() { /* ... */ }
function get_item() { /* ... */ }
function get_item_schema() { /* ... */ }
// and so on
add_action( 'rest_api_init', __NAMESPACE__ . '\\register_routes' );
```
While these shorter function names are simpler to work with, they don’t provide any other benefits over declaring global functions. For this reason the core REST API endpoints within WordPress are all implemented using a *controller class*. The remainder of this page details how to write your own controller class and explains the advantages of doing so. ## Controllers
A controller receives input (a `WP_REST_Request` object, in the case of the WordPress REST API) and generates response output as `WP_REST_Response` objects. Let’s look at an example controller class: ```php
class My_REST_Posts_Controller {
// Here initialize our namespace and resource name.
public function __construct() {
$this->namespace = '/my-namespace/v1';
$this->resource_name = 'posts';
}
// Register our routes.
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->resource_name, array(
// Here we register the readable endpoint for collections.
array(
'methods' => 'GET',
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
),
// Register our schema callback.
'schema' => array( $this, 'get_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->resource_name . '/(?P[\d]+)', array(
// Notice how we are registering multiple endpoints the 'schema' equates to an OPTIONS request.
array(
'methods' => 'GET',
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
),
// Register our schema callback.
'schema' => array( $this, 'get_item_schema' ),
) );
}
/**
* Check permissions for the posts.
*
* @param WP_REST_Request $request Current request.
*/
public function get_items_permissions_check( $request ) {
if ( ! current_user_can( 'read' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
}
return true;
}
/**
* Grabs the five most recent posts and outputs them as a rest response.
*
* @param WP_REST_Request $request Current request.
*/
public function get_items( $request ) {
$args = array(
'post_per_page' => 5,
);
$posts = get_posts( $args );
$data = array();
if ( empty( $posts ) ) {
return rest_ensure_response( $data );
}
foreach ( $posts as $post ) {
$response = $this->prepare_item_for_response( $post, $request );
$data[] = $this->prepare_response_for_collection( $response );
}
// Return all of our comment response data.
return rest_ensure_response( $data );
}
/**
* Check permissions for the posts.
*
* @param WP_REST_Request $request Current request.
*/
public function get_item_permissions_check( $request ) {
if ( ! current_user_can( 'read' ) ) {
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the post resource.' ), array( 'status' => $this->authorization_status_code() ) );
}
return true;
}
/**
* Gets post data of requested post id and outputs it as a rest response.
*
* @param WP_REST_Request $request Current request.
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$post = get_post( $id );
if ( empty( $post ) ) {
return rest_ensure_response( array() );
}
$response = $this->prepare_item_for_response( $post, $request );
// Return all of our post response data.
return $response;
}
/**
* Matches the post data to the schema we want.
*
* @param WP_Post $post The comment object whose response is being prepared.
*/
public function prepare_item_for_response( $post, $request ) {
$post_data = array();
$schema = $this->get_item_schema( $request );
// We are also renaming the fields to more understandable names.
if ( isset( $schema['properties']['id'] ) ) {
$post_data['id'] = (int) $post->ID;
}
if ( isset( $schema['properties']['content'] ) ) {
$post_data['content'] = apply_filters( 'the_content', $post->post_content, $post );
}
return rest_ensure_response( $post_data );
}
/**
* Prepare a response for inserting into a collection of responses.
*
* This is copied from WP_REST_Controller class in the WP REST API v2 plugin.
*
* @param WP_REST_Response $response Response object.
* @return array Response data, ready for insertion into collection data.
*/
public function prepare_response_for_collection( $response ) {
if ( ! ( $response instanceof WP_REST_Response ) ) {
return $response;
}
$data = (array) $response->get_data();
$server = rest_get_server();
if ( method_exists( $server, 'get_compact_response_links' ) ) {
$links = call_user_func( array( $server, 'get_compact_response_links' ), $response );
} else {
$links = call_user_func( array( $server, 'get_response_links' ), $response );
}
if ( ! empty( $links ) ) {
$data['_links'] = $links;
}
return $data;
}
/**
* Get our sample schema for a post.
*
* @return array The sample schema for a post
*/
public function get_item_schema() {
if ( $this->schema ) {
// Since WordPress 5.3, the schema can be cached in the $schema property.
return $this->schema;
}
$this->schema = array(
// This tells the spec of JSON Schema we are using which is draft 4.
'$schema' => 'http://json-schema.org/draft-04/schema#',
// The title property marks the identity of the resource.
'title' => 'post',
'type' => 'object',
// In JSON Schema you can specify object properties in the properties attribute.
'properties' => array(
'id' => array(
'description' => esc_html__( 'Unique identifier for the object.', 'my-textdomain' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'content' => array(
'description' => esc_html__( 'The content for the object.', 'my-textdomain' ),
'type' => 'string',
),
),
);
return $this->schema;
}
// Sets up the proper HTTP status code for authorization.
public function authorization_status_code() {
$status = 401;
if ( is_user_logged_in() ) {
$status = 403;
}
return $status;
}
}
// Function to register our new routes from the controller.
function prefix_register_my_rest_routes() {
$controller = new My_REST_Posts_Controller();
$controller->register_routes();
}
add_action( 'rest_api_init', 'prefix_register_my_rest_routes' );
```
## Benefits of Classes
This class contains all the same components you may have written using simple functions. The structure of a class gives us a convenient way to refer to related methods using the `$this->method_name()` syntax, but unlike a namespace the class also permits us to cache values and share logic. In the `get_item_schema` method, note that we store the generated schema on the class as `$this->schema`. Class properties make it easy to cache these sorts of generated values. The introduction of schema caching in WordPress 5.3 increased the speed of some core REST API collection responses by up to 40%, so you should definitely consider following this pattern in your own controllers. ## Class Inheritance & [WP\_REST\_Controller](https://developer.wordpress.org/reference/classes/wp_rest_controller/)
We’ve seen above how classes solve the global-function encapsulation issue, and how a class instance can be used to cache complex values to speed up response processing. The other major advantage of classes is the way in which class inheritance lets you share logic between multiple endpoints. Our example class here did not extend any base class, but within WordPress core all endpoint controllers extend a single `abstract` controller class called `WP_REST_Controller`. Extending this class gives you access to a number of useful methods, including but not limited to: - [`prepare_response_for_collection()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/prepare_response_for_collection/): Prepare a response for insertion into a collection.
- [`add_additional_fields_to_object()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/add_additional_fields_to_object/): append any registered REST fields to your prepared response object.
- [`get_fields_for_response()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/get_fields_for_response/): inspect the `_fields` query parameter to determine which response fields have been requested.
- [`get_context_param()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/get_context_param/): Retrieve the `context` parameter.
- [`filter_response_by_context()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/filter_response_by_context/): Filters the response shape based on the provided context parameter.
- [`get_collection_params()`](https://developer.wordpress.org/reference/classes/wp_rest_controller/get_collection_params/): return a basic set of parameter definitions useful for collection endpoints.
Endpoint-specific methods like `get_item`, `register_routes`, and `update_item_permissions_check` are not fully implemented by the abstract class, and must be defined in your own class. Visit the [`WP_REST_Controller` class reference page](https://developer.wordpress.org/reference/classes/wp_rest_controller/#methods) for a complete list of this controller’s methods. It is important to note that `WP_REST_Controller` is implemented as an `abstract` class and only contains logic that is clearly needed in multiple classes. Inheritance couples your class to the base class it extends, and poorly-considered inheritance trees can make your endpoints much harder to maintain. As an example, if you wrote a controller class for a posts endpoint (like the example above) and wanted to support custom post types as well, you should **NOT** extend your `My_REST_Posts_Controller` like this: `class My_CPT_REST_Controller extends My_REST_Posts_Controller`. Instead, you should either create an entirely separate base controller class for the shared logic, or make `My_REST_Posts_Controller` handle all available post types. Endpoint logic is subject to changing business requirements, and you don’t want to have to change a number of unrelated controllers every time you update your base posts controller. In most cases you will want to create a base controller class as either an `interface` or `abstract class` which each of your endpoint controllers can implement or extend, or to extend one of the core WordPress REST classes directly. ## Internal WordPress REST API Classes
The WordPress REST API follows a deliberate design pattern for its internal classes, which may be categorized as either *infrastructure* or *endpoint* classes. Endpoint classes encapsulate the functional logic necessary to perform [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations on WordPress resources. WordPress exposes many REST API endpoints (such as [`WP_REST_Posts_Controller`](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/)), but as discussed above all endpoints extend from a common base controller class: - [`WP_REST_Controller`](https://developer.wordpress.org/reference/classes/wp_rest_controller/): The base class for all WordPress core endpoints. This class is designed to represent a consistent pattern for manipulating WordPress resources. When interacting with an endpoint that implements `WP_REST_Controller`, a HTTP client can expect each endpoint to behave in a consistent way.
Infrastructure classes support the endpoint classes. They handle the logic for the WordPress REST API without performing any data transformation. The WordPress REST API implements three key infrastructure classes: - [`WP_REST_Server`](https://developer.wordpress.org/reference/classes/wp_rest_server): The main controller for the WordPress REST API. Routes are registered to the server within WordPress. When `WP_REST_Server` is called upon to serve a request, it determines which route is to be called, and passes the route callback a `WP_REST_Request` object. `WP_REST_Server` also handles authentication, and can perform request validation and permissions checks.
- [`WP_REST_Request`](https://developer.wordpress.org/reference/classes/wp_rest_request): An object to represent the nature of the request. This object includes request details like request headers, parameters, and method, as well as the route. It can also perform request validation and sanitization.
- [`WP_REST_Response`](https://developer.wordpress.org/reference/classes/wp_rest_response): An object to represent the nature of the response. This class extends `WP_HTTP_Response`, which includes headers, body, and status, and provides helper methods like `add_link()` for adding linked media, and `query_navigation_headers()` for getting query navigtion headers.
Most types of API-driven application will not require you to extend or interact directly with the infrastructure layer, but if you are implementing your own REST API endpoints your application will likely benefit from one or more endpoint controller classes which extend `WP_REST_Controller`.
---
# Requests
Source: https://developer.wordpress.org/rest-api/requests/
## Overview
While the WordPress REST API may be called internally within other WordPress code via PHP, the REST API is designed to be used remotely over HTTP. HTTP is the foundation for communication of data over the internet, and any application capable of HTTP requests may make use of the WordPress REST API, whether that application is a client-side JavaScript interface or an application on a remote server running in Python or Java.
The data structure of a request is conveniently handled by the `WP_REST_Request` class.
## [WP\_REST\_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
This class is one of the three main infrastructure classes introduced in WordPress 4.4. When an HTTP request is made to an endpoint of the API, the API will automatically create an instance of the `WP_REST_Request` class, matching the provided data. The response object is auto-generated in `WP_REST_Server`‘s `serve_request()` method. Once the request is created and authentication is checked, the request is dispatched and our endpoint callbacks begin to be fired. All of the data stored up in the `WP_REST_Request` object is passed into our callbacks for our registered endpoints. So both our `permission_callback` and `callback` are called with the request object being passed in. This enables us to access the various request properties in our callbacks, so that we can tailor our responses to match the desired output.
## Request Properties
Request objects have many different properties, each of which can be used in various ways. The main properties are the request method, route, headers, parameters and attributes. Let’s break each of these down into their role in a request. If you were to create a request object yourself it would look like this:
```php
$request = new WP_REST_Request( 'GET', '/my-namespace/v1/examples' );
```
In the above code sample we are only specifying that the request object method is `GET` and we should be matching the route `/my-namespace/v1/examples` which in the context of an entire URL would look like this: `https://ourawesomesite.com/wp-json/my-namespace/v1/examples`. The method and route arguments for the`[WP\_REST\_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)` constructor are used to map the request to the desired endpoint. If the request is made to an endpoint that is not registered then a helpful 404 error message is returned in the response. Let’s look at the various properties in more depth.
### Method
The method property of a request object by default matches the HTTP Request method. The method in most cases will be one of `GET`, `POST`, `PUT`, `DELETE`, `OPTIONS`, or `HEAD`. These methods will be used to match the various endpoints registered to a route. When the API finds a match for the method and route it will fire the callbacks for that endpoint.
The following convention is a best practice for matching HTTP methods: `GET` for read only tasks, `POST` for creation, `PUT` for updating, and `DELETE` for deleting. The request method acts as an indicator for the expected functionality of your endpoints. When you make a `GET` request to a route, you should expect to be returned read only data.
### Route
The route for a request, by default, will match the server environment variable for path info; `$_SERVER['PATH_INFO']`. When you make an HTTP request to a route of the WordPress REST API, the generated `WP_REST_Request` object will be made to match that path, which will hopefully then be matched to a valid endpoint. In short the route for a request is where you want to target your request in the API.
If we had registered a books endpoint, using `GET`, it might live at `https://ourawesomesite.com/wp-json/my-namespace/v1/books`. If we went to that URL in our browser, we would see our collection of books represented in JSON. WordPress will automatically generate the request object for us and handle all of the routing to match endpoints. So since we don’t really have to worry about the routing ourselves understanding how to pass extra data we want in our requests is a much more important thing to understand.
### Headers
HTTP Request headers are simply just extra data about our HTTP request. Request headers can specify caching policy, what our request content is, where the request is coming from and many other things. Request headers do not necessarily interact with our endpoints directly, but the information in the headers helps WordPress know what to do. To pass in data that we want our endpoints to interact with we want to use parameters.
### Parameters
When making requests to the WordPress REST API, most of the additional data passed in will take on the form of parameters. What are parameters? There are four different types in the context of the API. There are route parameters, query parameters, body parameters, and file parameters. Let’s take a look at each one a bit more in depth.
#### URL Params
URL parameters are automatically generated in a `WP_REST_Request` from the path variables in the requested route. What does that mean? Let’s look at this route, which grabs individual books by id: `/my-namespace/v1/books/(?P\d+)`. The odd looking `(?P\d+)` is a path variable. The name of the path variable is ‘`id`‘.
If we were to make a request like `GET https://ourawesomesite.com/wp-json/my-namespace/v1/books/5`,`5`will become the value for our`id`path variable. The`[WP\_REST\_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)` object will automatically take that path variable and store it as a URL parameter. Now inside of our endpoint callbacks we can interact with that URL parameter really easily. Let’s look at an example.
```php
// Register our individual books endpoint.
function prefix_register_book_route() {
register_rest_route( 'my-namespace/v1', '/books/(?P\d+)', array(
// Supported methods for this endpoint. WP_REST_Server::READABLE translates to GET.
'methods' => WP_REST_Server::READABLE,
// Register the callback for the endpoint.
'callback' => 'prefix_get_book',
) );
}
add_action( 'rest_api_init', 'prefix_register_book_route' );
/**
* Our registered endpoint callback. Notice how we are passing in $request as an argument.
* By default, the WP_REST_Server will pass in the matched request object to our callback.
*
* @param WP_REST_Request $request The current matched request object.
*/
function prefix_get_book( $request ) {
// Here we are accessing the path variable 'id' from the $request.
$book = prefix_get_the_book( $request['id'] );
return rest_ensure_response( $book );
}
// A simple function that grabs a book title from our books by ID.
function prefix_get_the_book( $id ) {
$books = array(
'Design Patterns',
'Clean Code',
'Refactoring',
'Structure and Interpretation of Computer Programs',
);
$book = '';
if ( isset( $books[ $id ] ) ) {
// Grab the matching book.
$book = $books[ $id ];
} else {
// Error handling.
return new WP_Error( 'rest_not_found', esc_html__( 'The book does not exist', 'my-text-domain' ), array( 'status' => 404 ) );
}
return $book;
}
```
In the example above we see how path variables are stored as URL parameters in the request object. We can then access those parameters in our endpoint callbacks. The above example is a pretty common use case for using URL params. Adding too many path variables to a route can slow down the matching of routes and it can also over complicate registering endpoints, it is advised to use URL parameters sparingly. If we aren’t supposed to use parameters directly in our URL path, then we need another way to pass in extra information to our request. This is where query and body parameters come in, they will typically do most of the heavy lifting in your API.
#### Query Params
Query parameters exist in the query string portion of a URI. The query string portion of a URI in `https://ourawesomesite.com/wp-json/my-namespace/v1/books?per_page=2&genre=fiction` is`?per\_page=2&genre=fiction`. The query string is started by the '`?`' character, the different values within the query string are separated by the '`&`' character. We specified two parameters in our query string;`per\_page`and`genre`. In our endpoint we would want to grab only two books from the fiction genre. We could access those values in a callback like this:`$request\[‘per\_page’\]`, and`$request\[‘genre’\]` ( assuming $request is the name of the argument we are using ). If you are familiar with PHP you have probably used query parameters in your web applications.
In PHP, the query parameters get stored in the superglobal `$_GET`. It is important to note that you should never directly access any superglobals or server variables in your endpoints. It is always best to work with what is provided by the `WP_REST_Request` class. Another common method for passing in variables to an endpoint is to use body parameters.
#### Body Params
Body parameters are key value pairs that are stored in the request body. If you have ever sent a `POST` request via a `