Setting up the custom route

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.

Route namespace

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

Get Learning WordPress REST API now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.