WordPress REST API is powerful, but many endpoints are built without validation and permission checks. Here’s a clean pattern you can reuse.
Recommended approach
- Register routes with
register_rest_route. - Validate and sanitize input (never trust request data).
- Use a
permission_callbackfor auth/authorization.
Example endpoint (skeleton)
add_action('rest_api_init', function () {
register_rest_route('vistosys/v1', '/status', [
'methods' => 'GET',
'callback' => function () {
return rest_ensure_response([ 'ok' => true ]);
},
'permission_callback' => '__return_true',
]);
});
Security tips
- If it changes data, require authentication.
- Rate-limit sensitive endpoints (login, reset password, signup).
- Return minimal data (avoid leaking user details).
Once your pattern is stable, you can add caching (transients/object cache) for GET endpoints.