We will be using the register_rest_route()
function for defining a custom route in a function that will be then hooked up to the rest_api_init
action, which will run upon the initialization of REST API. It will be equally important for an action, just like init
and plugin_loaded
are.
This function will accept the following four arguments:
Route namespace
URL after namespace
Endpoints to a route
Optional Boolean argument
We will look into the four arguments briefly as follows.
The first argument would the namespace for the route. It is important to understand that each and every route must be namespaced, which will further be used after the wp-JSON
as the next URL segment. All default routes are namespaced with wp
. Every core route will have URLs like wp-JSON/wp/posts
, while custom route colors within the my-shirt-shop
namespace will have the attribute URL as wp-JSON/my-shirt-shop/colors
.
Those acting like PHP namespaces or unique slugs as functions for...