1.3 KiB
1.3 KiB
Routes and Endpoints (summary)
Registering routes
- Register routes on the
rest_api_inithook withregister_rest_route( $namespace, $route, $args ). - A route is the URL pattern; an endpoint is the method + callback bound to that route.
- For non-pretty permalinks, the route is accessed via
?rest_route=/namespace/route.
Namespacing
- Always namespace routes (
vendor/v1). - Do not use the
wp/*namespace unless you are targeting core.
Methods
- Use
WP_REST_Server::READABLE(GET),CREATABLE(POST),EDITABLE(PUT/PATCH),DELETABLE(DELETE). - Multiple endpoints can share a route, one per method.
permission_callback (required)
- Always provide
permission_callback. - Public endpoints should use
__return_true. - For restricted endpoints, use capability checks (
current_user_can) or object-level authorization. - Missing
permission_callbackemits a_doing_it_wrongnotice in modern WP.
Arguments
- Register
argsto validate and sanitize inputs. - Use
type,required,default,validate_callback,sanitize_callback. - Access params via the
WP_REST_Requestobject, not$_GET/$_POST.
Return values
- Return data via
rest_ensure_response()or aWP_REST_Response. - Return
WP_Errorwith astatusindatafor error responses. - Do not call
wp_send_json()in REST callbacks.