The Front Door You Forgot You Built: What These 5 REST API Hooks Actually Gate
The Problem Every WordPress install answers REST requests at /wp-json/ whether or not anyone deliberately built an API — and most sites have at least one custom field,…

Filter Hook since 4.4.0
Short-circuits a REST API request before routing
rest_pre_dispatch lets you intercept a REST API request before WordPress even matches it to a route — the earliest point to block or fake a response.
The very first REST API filter to run, before the route is matched — earlier than any permission_callback or route callback.
apply_filters( 'rest_pre_dispatch', mixed $result, WP_REST_Server $server, WP_REST_Request $request );$result mixed — Response to replace the requested route with, null by default.$server WP_REST_Server — The REST server instance.$request WP_REST_Request — The current request.add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
if ( str_starts_with( $request->get_route(), '/my-plugin/v1' ) && ! is_user_logged_in() ) {
return new WP_Error( 'forbidden', 'Login required', [ 'status' => 401 ] );
}
return $result;
}, 10, 3 );Block an entire route namespace for logged-out users, before any route-specific logic runs.
add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
if ( '/wp/v2/posts' === $request->get_route() ) {
$cached = get_transient( 'rest_posts_cache' );
if ( false !== $cached ) {
return rest_ensure_response( $cached );
}
}
return $result;
}, 10, 3 );Serve a cached response for a high-traffic read-only endpoint without hitting the database.

The Problem Every WordPress install answers REST requests at /wp-json/ whether or not anyone deliberately built an API — and most sites have at least one custom field,…
permission_callback runs per-route after matching, for auth checks on that specific endpoint. rest_pre_dispatch runs before routing even happens and can intercept every request site-wide.
Source: wp-includes/rest-api/class-wp-rest-server.php
One session: this hook, then each related_hooks entry. Walk it and tell us what to change.
rest_pre_dispatch → rest_api_init → rest_prepare_post
Practice the rest_pre_dispatch WordPress filter hook in our interactive sandbox — a VS Code-style editor with a step-by-step rest_pre_dispatch tutorial, add_filter exercises, and instant feedback. No local WordPress install required.