Lesson 1.1. What the WordPress REST API Is

"A route is not a URL"

Our 4WP Account plugin signs users in with OAuth. The callback URL was built like this:

PHP
$callback = home_url( '/wp-json/forwp-account/v1/callback' );

It worked on every site we tested. wordpress.org review sent it back anyway: on a site with plain permalinks there is no /wp-json/ path, and the same route lives at /?rest_route=/forwp-account/v1/callback. The fix was one function:

PHP
$callback = rest_url( 'forwp-account/v1/callback' );

To get REST right, you first need to know what lives at /wp-json/ and who builds its URLs.

Goal

In 10 minutes, understand what the WordPress REST API is, how it is organised into namespaces, routes and endpoints, how a client discovers it, and why you always let WordPress build REST URLs.

REST in one sentence

REST is resources at URLs, verbs from HTTP, and JSON in between. A post is a resource at /wp/v2/posts/123. GET reads it, POST, PUT or PATCH change it, DELETE removes it. The answer is JSON plus a status code.

WordPress has shipped the REST API in core since 4.7. It powers the block editor, so every modern site already serves it.

The /wp-json/ index

Open https://your-site.test/wp-json/ and WordPress describes itself:

JSON
{
  "name": "My Site",
  "url": "https://your-site.test",
  "namespaces": [
    "oembed/1.0",
    "forwp-faq/v1",
    "wp/v2",
    "wp-site-health/v1",
    "wp-block-editor/v1"
  ],
  "routes": { "/": { … }, "/wp/v2/posts": { … }, … }
}
  • namespaces — who owns which group of routes. wp/v2 is core's. forwp-faq/v1 appears only while 4WP FAQ is active.
  • routes — every route with its methods and arguments. Your API documents itself.
  • /wp-json/wp/v2 lists only one namespace's routes.

Namespace, route, endpoint

flowchart LR
  NS["Namespace<br/>wp/v2"] --> RT["Route<br/>/wp/v2/posts/(?P<id>\d+)"]
  RT --> E1["Endpoint<br/>GET → read"]
  RT --> E2["Endpoint<br/>POST/PUT/PATCH → update"]
  RT --> E3["Endpoint<br/>DELETE → delete"]
  • Namespace = vendor + version: wp/v2, forwp-faq/v1. Plugins never register under wp/v2.
  • Route = the path pattern.
  • Endpoint = a route + an HTTP method + a callback. One route usually has several endpoints.

Discovery

A client does not have to guess where the API is. Every front-end page carries a link to it:

TEXT
Link: <https://your-site.test/wp-json/>; rel="https://api.w.org/"

The same link is printed in the page <head>. That is why a client should read it instead of hardcoding /wp-json/.

PermalinksRoute URL
Pretty (/%postname%/)https://site.test/wp-json/wp/v2/posts
Plain (?p=123)https://site.test/?rest_route=/wp/v2/posts

rest_url() and get_rest_url() know which one applies. In JavaScript, localize the value from PHP.

Key concepts

  • The REST API is in core and answers at /wp-json/ on every site
  • Namespace = owner + version; wp/v2 belongs to core
  • Route = path; endpoint = route + method + callback
  • The index lists every namespace and route with its arguments
  • Never hardcode /wp-json/ — use rest_url()

Code / requests

BASH
# The index
curl -s https://your-site.test/wp-json/ | head -c 600

# Namespaces only (needs jq)
curl -s https://your-site.test/wp-json/ | jq '.namespaces'

# Discovery header
curl -sI https://your-site.test/ | grep -i 'api.w.org'
PHP
// PHP: let WordPress build the URL
$url = rest_url( 'wp/v2/posts' );

Common mistakes

SymptomCauseFix
404 on /wp-json/ on one client siteplain permalinksrest_url() or /?rest_route=
Plugin namespace missing from the indexthe plugin is inactive or routes register outside rest_api_initactivate it, register on rest_api_init
"My endpoint" vs "my route" mixed up in a review answerterms used looselyroute = path, endpoint = path + method

Practice

Open the index of your local site, find wp/v2 and one plugin namespace, then open the same route through /?rest_route=.

<!– Practice terminal (Postman style): later, see 00-angle.md –>

Interview Questions

A route is the URL pattern, for example /wp/v2/posts/(?P<id>\d+). An endpoint is that route combined with an HTTP method and a callback: GET on it reads a post, DELETE on it deletes one. One route usually exposes several endpoints.

A namespace groups routes under an owner and a version, like wp/v2 or forwp-faq/v1. wp/v2 belongs to core; a plugin that registers there can collide with core or other plugins, and it cannot version its own API. Its own namespace with a version lets it change the API later without breaking clients.

Through discovery: WordPress sends a Link header with rel="https://api.w.org/" and prints the same link in the page head. The client follows it to the /wp-json/ index, which lists every namespace and route.

Because /wp-json/ exists only with pretty permalinks. With plain permalinks the route is at /?rest_route=…. rest_url() returns the right URL for the site's settings, and it respects filters such as a custom REST prefix.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.