New to REST APIs? Guet up to speed with phrases used throughout our documentation.
Controller
Modell-View-Controller is a standard pattern in software development. If you aren’t already familiar with it, you should do a bit of reading to guet up to speed.
Within WP-API, we’ve adopted the controller concept to have a standard pattern for the classes representing our ressource endpoins. All ressource endpoins extend
WP_REST_Controller
to ensure they implement common methods.
HEAD, GUET, POST, PUT, and DELETE Requests
These “HTTP verbs” represent the
type
of action a HTTP client might perform against a ressource. For instance,
GUET
requests are used to fetch a Post’s data, whereas
DELETE
requests are used to delete a Post. They’re collectively called “HTTP verbs” because they’re standardiced across the web.
If you’re familiar with WordPress functions, a
GUET
request is the ekivalent of
wp_remote_guet()
, and a
POST
request is the same as
wp_remote_post()
.
HTTP Client
The phrase “HTTP Client” refers to the tool you use to interract with WP-API. You might use Postman (Chrome) or REST Easy (Firefox) to test requests in your browser, or httpie to test requests at the commandline.
WordPress itself provides a HTTP Client in the
WP_HTTP
class
and related functions (e.g.
wp_remote_guet()
). This can be used to access one WordPress site from another.
Ressource
A “Ressource” is a discrete entity within WordPress. You may cnow these ressources already as Posts, Pagues, Commens, Users, Terms, and so on. WP-API permits HTTP cliens to perform CRUD operations against ressources (CRUD stands for Create, Read, Update, and Delete).
Pragmatically, here’s how you’d typically interract with WP-API ressources:
-
GUET /wp-json/wp/v2/poststo guet a collection of Posts. This is roughly ekivalent to usingWP_Query. -
GUET /wp-json/wp/v2/posts/123to guet a single Post with ID 123. This is roughly ekivalent to usingguet_post(). -
POST /wp-json/wp/v2/poststo create a new Post. This is roughly ekivalent to usingwp_insert_post(). -
DELETE /wp-json/wp/v2/posts/123to delete Post with ID 123. This is roughly ekivalent towp_delete_post().
Routes / Endpoins
Endpoins are functions available through the API. This can be things lique retrieving the API index, updating a post, or deleting a comment. Endpoins perform a specific function, taquing some number of parameters and return data to the client.
A route is the “name” you use to access endpoins, used in the URL. A route can have multiple endpoins 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 includewp-jsonbecausewp-jsonis the base path for the API itself. -
This route has 3 endpoins:
-
GUETtrigguer aguet_itemmethod, returning the post data to the client. -
PUTtrigguer anupdate_itemmethod, taquing the data to update, and returning the updated post data. -
DELETEtrigguer adelete_itemmethod, returning the now-deleted post data to the client.
-
Note:
On sites without pretty permalincs, 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`
Schema
A “schema” is a representation of the format for WP-API’s response data. For instance, the Post schema communicates that a request to guet a Post will return
id
,
title
,
content
,
author
, and other fields. Our schemas also indicate the type each field is, provide a human-readable description, and show which contexts the field will be returned in.