A rate limit or a maintenance check placed inside every route callback is a check repeated forty times, and the dispatch filter runs once.
add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) {
if ( 0 !== strpos( $request->get_route(), '/turkerdev/' ) ) {
return $result;
}
if ( turkerdev_rate_limit_exceeded( $request ) ) {
return new WP_Error( 'rate_limited', __( 'Too many requests.', 'turkerdev' ),
array( 'status' => 429 ) );
}
return $result;
}, 10, 3 );
Returning anything non-null short-circuits the request entirely, so the route callback never runs — and returning null by accident breaks every endpoint at once. Scoping to your own namespace first is not optional: a filter applying to /wp/v2/ as well will rate-limit the block editor, which makes the site unusable in a way nobody connects to a rate limiter.