Harden the WordPress REST API without breaking the editor
“Disable the REST API” is common advice and wrong: the block editor, the Site Editor, many plugins and WooCommerce’s checkout blocks run on it. What can be done is smaller and safer: hide the routes that leak data to anonymous visitors, remove routes of features the site does not use, require authentication for custom routes that should not be public, and limit the request rate on the web server.
Problem: A security scan reports that /wp-json/wp/v2/users lists author names and slugs, that the index at /wp-json/ shows every plugin’s routes, and that a custom export route of a plugin answers without login.
Solution: Restrict the users endpoints to logged-in users with the rest_endpoints filter, remove the routes of unused features for visitors only, give every custom route a real permission_callback, and add rate limiting on /wp-json/ in Nginx. Leave the API itself enabled.
Users: only for logged-in users
add_filter( 'rest_endpoints', 'ha_rest_restrict_users' );
function ha_rest_restrict_users( array $endpoints ): array {
if ( is_user_logged_in() ) {
return $endpoints; // the editor needs these routes (author picker, mentions)
}
foreach ( array_keys( $endpoints ) as $route ) {
if ( str_starts_with( $route, '/wp/v2/users' ) ) {
unset( $endpoints[ $route ] );
}
}
return $endpoints;
}
Anonymous requests to /wp-json/wp/v2/users now get a 404 rest_no_route. The users list was the source of user names for brute-force attacks; the other places that reveal them (author archives, ?author=1, login error messages) are covered in prevent username enumeration.
is_user_logged_in() works in REST requests because the REST API authenticates the cookie with the X-WP-Nonce header before the routes are dispatched. Without a valid nonce, a logged-in browser counts as anonymous in REST, which is the intended behavior.
Routes of unused features
add_filter( 'rest_endpoints', 'ha_rest_remove_unused' );
function ha_rest_remove_unused( array $endpoints ): array {
if ( current_user_can( 'edit_posts' ) ) {
return $endpoints;
}
$prefixes = array( '/wp/v2/comments', '/oembed/1.0/proxy' );
foreach ( array_keys( $endpoints ) as $route ) {
foreach ( $prefixes as $prefix ) {
if ( str_starts_with( $route, $prefix ) ) {
unset( $endpoints[ $route ] );
}
}
}
return $endpoints;
}
Remove only what the front end certainly does not use: comments on a site with comments disabled, for example. Themes and blocks call routes on the front end too (search, Query Loop with JavaScript, WooCommerce Store API for the cart), so test the front end as a logged-out visitor after every change. Editors keep all routes.
Custom routes: always a real permission check
register_rest_route( 'ha/v1', '/export', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'ha_rest_export',
'permission_callback' => fn() => current_user_can( 'manage_options' ),
) );
Since WordPress 5.5, a route without permission_callback triggers a notice, and many plugins “fix” it with __return_true, which makes the route public. Every route that returns non-public data or changes something needs a capability check here, not inside the callback. Endpoints with authentication and schema validation: custom REST API endpoints.
Rate limiting in front of PHP
Repeated requests to expensive routes (search, product lists with filters) are a load problem more than a data problem. Limiting them per IP on the web server costs nothing for normal visitors: Nginx rate limiting for the REST API.
What not to do
add_filter( 'rest_authentication_errors', ... )that rejects all anonymous requests breaks oEmbed, front-end blocks, WooCommerce’s cart and checkout blocks, and many contact forms.- Removing the
/wp-json/index or theLinkheader only hides the API from honest clients; the routes still answer. - Security plugins with a “disable REST API” switch usually implement the first point; check what exactly they block.
NOTE: After restricting routes, check the browser console in the block editor and on key front-end pages for 401 and 404 errors from /wp-json/. The REST API also answers at /?rest_route=/wp/v2/users; the rest_endpoints filter covers both URLs, a web server rule on /wp-json/ alone does not.