"A route is not a URL"
Our 4WP Account plugin signs users in with OAuth. The callback URL was built like this:
$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:
$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:
{
"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/v2is core's.forwp-faq/v1appears only while 4WP FAQ is active.routes— every route with its methods and arguments. Your API documents itself./wp-json/wp/v2lists 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 underwp/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:
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/.
Pretty vs plain permalinks
| Permalinks | Route 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/v2belongs to core - Route = path; endpoint = route + method + callback
- The index lists every namespace and route with its arguments
- Never hardcode
/wp-json/— userest_url()
Code / requests
# 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: let WordPress build the URL
$url = rest_url( 'wp/v2/posts' );
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
404 on /wp-json/ on one client site | plain permalinks | rest_url() or /?rest_route= |
| Plugin namespace missing from the index | the plugin is inactive or routes register outside rest_api_init | activate it, register on rest_api_init |
| "My endpoint" vs "my route" mixed up in a review answer | terms used loosely | route = 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.