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
Filters whether a REST API request should be blocked as unauthenticated
rest_authentication_errors runs after WordPress's own authentication checks (cookie, application passwords) and is the standard hook for adding custom auth schemes or short-circuiting requests with an error.
Runs during REST API bootstrap, after core's built-in authentication handlers have already run and set their result.
apply_filters( 'rest_authentication_errors', WP_Error|null|bool $errors );errors WP_Error|null|bool — WP_Error if already blocked, null if no error yet, true if already authenticated.add_filter( 'rest_authentication_errors', function( $errors ) {
error_log( 'REST auth check ran.' );
return $errors;
} );Log every REST authentication check.
add_filter( 'rest_authentication_errors', function( $errors ) {
if ( ! empty( $errors ) ) {
return $errors; // don't override an existing error
}
if ( empty( $_SERVER['HTTP_X_API_KEY'] ) || ! my_api_key_is_valid( $_SERVER['HTTP_X_API_KEY'] ) ) {
return new WP_Error( 'rest_forbidden', 'Missing or invalid API key.', array( 'status' => 401 ) );
}
return $errors;
} );Require a custom API key header for headless/REST access.
add_filter( 'rest_authentication_errors', function( $errors ) {
if ( is_wp_error( $errors ) ) {
return $errors; // never clear an existing error
}
return $errors;
} );Always check for and preserve an existing WP_Error before adding your own logic — overwriting it silently reopens a blocked request.

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,…
null means 'no opinion, defer to other checks'; a WP_Error means 'block this request with this specific error'.
Yes — it's the standard extension point for custom REST authentication schemes like API keys or signed requests.
Source: wp-includes/rest-api.php
One session: this hook, then each related_hooks entry. Walk it and tell us what to change.
rest_authentication_errors → rest_api_init → rest_insert_post
Practice the rest_authentication_errors WordPress filter hook in our interactive sandbox — a VS Code-style editor with a step-by-step rest_authentication_errors tutorial, add_filter exercises, and instant feedback. No local WordPress install required.