A WordPress developer adds a custom REST endpoint with register_rest_route(), ships it, and a week later the site is hit by 40,000 requests an hour scraping the endpoint. The route was unauthenticated, returned 200KB of JSON each time, and had no rate limit. The six-line example in the WordPress documentation produces a working endpoint; production-shaped endpoints handle auth, validation, caching, and abuse.
What’s happening
The WordPress REST API is the core HTTP surface for everything from Gutenberg saves to mobile apps. Custom routes register with a namespace and an endpoint, declare an HTTP method, a callback, and a permission_callback. The permission_callback is the line most tutorials skip — it’s what stands between an endpoint and the open internet. If it returns __return_true, the endpoint is public.
The other piece is schema. A register_rest_route() call without args lets WordPress pass anything to the callback. Add args with type, required, sanitize_callback, and validate_callback, and WordPress rejects malformed input before the callback ever runs.
How to verify
Inventory the custom endpoints on a site:
wp eval '
foreach (rest_get_server()->get_routes() as $route => $handlers) {
if (preg_match("#^/(wp|oembed|wc|wcfm)/#", $route)) continue;
foreach ($handlers as $h) {
echo $route . " [" . implode(",", array_keys($h["methods"])) . "] ";
echo "perm:" . (is_string($h["permission_callback"]) ? $h["permission_callback"] : "closure") . "\n";
}
}
'
Anything where perm:__return_true is public. For each, check what it returns:
curl -s "https://example.com/wp-json/myplugin/v1/data" | jq | head
If the response is sensitive (user emails, internal IDs, draft post content), the endpoint is leaking. If it’s public-by-design, confirm the rate limit story below.
What to do
Build the endpoint with auth, schema, and rate limiting from the start.
- The minimal shape that’s actually production-ready:
add_action('rest_api_init', function() {
register_rest_route('myplugin/v1', '/products/(?P<id>\d+)', [
'methods' => WP_REST_Server::READABLE, // GET
'callback' => 'myplugin_get_product',
'permission_callback' => 'myplugin_can_read',
'args' => [
'id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => function($v) {
return is_numeric($v) && $v > 0;
},
],
],
]);
});
function myplugin_can_read(WP_REST_Request $request) {
// 1. Application passwords / nonces for authenticated users
if (is_user_logged_in() && current_user_can('read')) return true;
// 2. API key for service-to-service
$key = $request->get_header('X-API-Key');
if ($key && hash_equals(MYPLUGIN_API_KEY, $key)) return true;
return new WP_Error('rest_forbidden', 'Authentication required', ['status' => 401]);
}
function myplugin_get_product(WP_REST_Request $request) {
$product_id = $request['id'];
// ... fetch ...
return rest_ensure_response($product);
}
- For public endpoints (no auth required), put a rate limit in front. WordPress doesn’t ship one — use Cloudflare’s rate-limit rules, a WAF rule, or an in-PHP transient counter:
function myplugin_rate_limit() {
$ip = $_SERVER['REMOTE_ADDR'];
$key = 'rl_' . md5($ip);
$hits = (int) get_transient($key);
if ($hits > 60) {
return new WP_Error('rest_rate_limit', 'Too many requests', ['status' => 429]);
}
set_transient($key, $hits + 1, 60);
return true;
}
Wire it into permission_callback so it runs before the callback executes.
- Cache the response at the page-cache layer. REST endpoints aren’t cached by default. Add a header that your CDN respects:
function myplugin_get_product($request) {
$response = rest_ensure_response($product);
$response->header('Cache-Control', 'public, max-age=300');
return $response;
}
For Cloudflare APO or BunnyCDN, that header is enough. For nginx FastCGI cache, add a cache_keys filter so the REST URL is part of the key.
-
Validate everything. Use
sanitize_callbackfor “clean this up” (strip, absint, sanitize_text_field) andvalidate_callbackfor “reject if invalid.” The args declaration is a schema — treat it like one. -
For internal-only endpoints, require an application password or a nonce. Application passwords are how mobile apps and external services authenticate; nonces work for AJAX-style requests from the WordPress front-end.
-
For Gutenberg block dev, the REST API is also where blocks fetch and save. Routes for editors should use
current_user_can('edit_posts')or the post-specific cap — never__return_true. -
Document the endpoints with the
schemaargument —register_rest_route()accepts aschemacallable that returns a JSON schema. Tools like the Postman generator and OpenAPI exporters read it.
Stack Harbor’s Enterprise WordPress practice ships REST API audits, rate-limit configurations, and custom-endpoint reviews as standard. If a site has an unaudited custom REST surface, we can do the review.