Skip to main content

WP_REST_Controller

This is a copy of WP_REST_Controller which is not currently in the WordPress core.

https://github.com/WP-API/WP-API/blob/develop/lib/endpoints/class-wp-rest-controller.php

Last updated 17 August 2016

Class WP_REST_Controller

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:15

Details

  • Kind: class
  • Namespace: (global)

Properties

PropertyTypeDescription
$namespacestringThe namespace of this controller's route.
$rest_basestringThe base of this controller's route.

Methods

MethodDescription
register_routes()Register the routes for the objects of the controller.
get_items_permissions_check()Check if a given request has access to get items.
get_items()Get a collection of items.
get_item_permissions_check()Check if a given request has access to get a specific item.
get_item()Get one item from the collection.
create_item_permissions_check()Check if a given request has access to create items.
create_item()Create one item from the collection.
update_item_permissions_check()Check if a given request has access to update a specific item.
update_item()Update one item from the collection.
delete_item_permissions_check()Check if a given request has access to delete a specific item.
delete_item()Delete one item from the collection.
prepare_item_for_response()Prepare the item for the REST response.
prepare_response_for_collection()Prepare a response for inserting into a collection.
filter_response_by_context()Filter a response based on the context defined in the schema.
get_item_schema()Get the item's schema, conforming to JSON Schema.
get_public_item_schema()Get the item's schema for display / public consumption purposes.
get_collection_params()Get the query params for collections.
get_context_param()Get the magical context param.
get_endpoint_args_for_item_schema()Get an array of endpoint arguments from the item schema for the controller.
get_post()Retrieves post data given a post ID or post object.

Method Reference

register_routes()

public function register_routes()

Register the routes for the objects of the controller.

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:43

get_items_permissions_check()

public function get_items_permissions_check( $request )

Check if a given request has access to get items.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:57

get_items()

public function get_items( $request )

Get a collection of items.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:71

get_item_permissions_check()

public function get_item_permissions_check( $request )

Check if a given request has access to get a specific item.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:85

get_item()

public function get_item( $request )

Get one item from the collection.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:99

create_item_permissions_check()

public function create_item_permissions_check( $request )

Check if a given request has access to create items.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:113

create_item()

public function create_item( $request )

Create one item from the collection.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:127

update_item_permissions_check()

public function update_item_permissions_check( $request )

Check if a given request has access to update a specific item.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:141

update_item()

public function update_item( $request )

Update one item from the collection.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:155

delete_item_permissions_check()

public function delete_item_permissions_check( $request )

Check if a given request has access to delete a specific item.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:169

delete_item()

public function delete_item( $request )

Delete one item from the collection.

Parameters

NameTypeDefaultDescription
$requestWP_REST_RequestFull data about the request.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:183

prepare_item_for_response()

public function prepare_item_for_response( $item, $request )

Prepare the item for the REST response.

Parameters

NameTypeDefaultDescription
$itemmixedWordPress representation of the item.
$requestWP_REST_RequestRequest object.

Returns

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:212

prepare_response_for_collection()

public function prepare_response_for_collection( $response )

Prepare a response for inserting into a collection.

Parameters

NameTypeDefaultDescription
$responseWP_REST_ResponseResponse object.

Returns

  • array — Response data, ready for insertion into collection data.

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:226

filter_response_by_context()

public function filter_response_by_context( $data, $context )

Filter a response based on the context defined in the schema.

Parameters

NameTypeDefaultDescription
$dataarray
$contextstring

Returns

  • array

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:258

get_item_schema()

public function get_item_schema()

Get the item's schema, conforming to JSON Schema.

Returns

  • array

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:295

get_public_item_schema()

public function get_public_item_schema()

Get the item's schema for display / public consumption purposes.

Returns

  • array

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:307

get_collection_params()

public function get_collection_params()

Get the query params for collections.

Returns

  • array

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:328

get_context_param()

public function get_context_param( $args=[] )

Get the magical context param.

Ensures consistent description between endpoints, and populates enum from schema.

Parameters

NameTypeDefaultDescription
$argsarray[]

Returns

  • array

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:369

get_endpoint_args_for_item_schema()

public function get_endpoint_args_for_item_schema( $method=WP_REST_Server::CREATABLE )

Get an array of endpoint arguments from the item schema for the controller.

Parameters

NameTypeDefaultDescription
$methodstringWP_REST_Server::CREATABLEHTTP method of the request. The arguments for CREATABLE requests are checked for required values and may fall-back to a given default, this is not done on EDITABLE requests. Default is WP_REST_Server::CREATABLE.

Returns

  • array — $endpoint_args

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:544

get_post()

public function get_post( $post )

Retrieves post data given a post ID or post object.

This is a subset of the functionality of the get_post() function, with the additional functionality of having the_post action done on the resultant post object. This is done so that plugins may manipulate the post that is used in the REST API.

Parameters

NameTypeDefaultDescription
$postint | WP_PostPost ID or post object. Defaults to global $post.

Returns

  • WP_Post | null — A WP_Post object when successful.

See Also

Since: 2.4-beta-1

Source: includes/webapi/v2/includes/controllers/class-wp-rest-controller.php:613