WordPress answers REST API requests from anyone by default. Visit /wp-json/wp/v2/users on most sites and you get the name and URL slug of everyone who has published a post. The slug is often the same as the login username. You can restrict WordPress REST API access to logged-in users with one filter, but the usual snippet also breaks the WooCommerce Cart and Checkout blocks, Contact Form 7 and embeds, because they use the same API for visitors.
This post gives you a version that keeps those routes working, a smaller fix if you only want to hide usernames, and the checks to run before you put either on a live site.
Restrict the WordPress REST API to logged-in users
Add this to a child theme’s functions.php or a small plugin:
<?php
/**
* Restrict the WordPress REST API to logged-in users, but keep the public routes your site needs.
*/
add_filter( 'rest_authentication_errors', 'dp_rest_require_login' );
function dp_rest_require_login( $result ) {
// Another check already decided, or the visitor is logged in: leave it alone.
if ( true === $result || is_wp_error( $result ) || is_user_logged_in() ) {
return $result;
}
$route = '/' . ltrim( (string) ( $GLOBALS['wp']->query_vars['rest_route'] ?? '' ), '/' );
// Routes that must stay public. Add or remove prefixes for your site.
$public = apply_filters( 'dp_rest_public_routes', array(
'/wc/store/', // WooCommerce Cart and Checkout blocks.
'/contact-form-7/v1/', // Contact Form 7 form submissions.
'/oembed/1.0/', // Embeds of your posts on other sites.
) );
foreach ( $public as $prefix ) {
if ( 0 === strpos( $route, $prefix ) ) {
return $result;
}
}
return new WP_Error( 'rest_not_logged_in', __( 'You must be logged in to use the REST API.' ), array( 'status' => 401 ) );
}WordPress runs the rest_authentication_errors filter on every REST request that comes in over HTTP. Returning a WP_Error stops the request with that status, so visitors who aren’t logged in get a 401. Logged-in users, and apps that sign in with an application password, are not affected.
The list of prefixes is the part you need to change for your site. Each entry keeps every route that starts with it public. You can also change the list from another plugin with the dp_rest_public_routes filter.
What restricting the REST API breaks
Anything that calls the REST API for a visitor who isn’t logged in stops working unless its route is in the list. These are the common ones:
| Feature | Route | In the list above? |
|---|---|---|
| WooCommerce Cart and Checkout blocks | /wc/store/ | Yes. Without it, guests can’t add to cart or check out. |
| Contact Form 7 submissions | /contact-form-7/v1/ | Yes. |
| Embeds of your posts on other sites | /oembed/1.0/ | Yes. |
| A headless front end or app that reads posts without signing in | usually /wp/v2/ | No. Sign the app in with an application password, or add only the routes it reads. |
| Other plugins with front-end features (live search, filters, forms) | their own namespace | Check your site, as below. |
The block editor, Site Health and the rest of wp-admin keep working, because you are logged in when you use them. Server-side code that calls rest_do_request() is not affected either: the filter only runs for requests that arrive over HTTP.
Find the routes your site needs
Before you add the snippet to a live site, find out which routes visitors use:
- Open your site in a private window, open the browser’s developer tools and filter the Network tab by
wp-json. Then add a product to the cart, check out, send a form and use any search or filter. Every request you see needs its prefix in the list. - Search your server’s access log for
wp-jsonandrest_route=to see what has been called over the past few weeks.
If you only want to hide usernames
If user enumeration is your only concern, you don’t need to close the whole API. This removes the users endpoints for visitors who aren’t logged in and leaves everything else alone:
<?php
/**
* Hide the users endpoints of the REST API from visitors who aren't logged in.
*/
add_filter( 'rest_endpoints', 'dp_rest_hide_users_from_guests' );
function dp_rest_hide_users_from_guests( $endpoints ) {
if ( ! is_user_logged_in() ) {
unset( $endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
}Visitors get a 404 for /wp-json/wp/v2/users and /wp-json/wp/v2/users/1; logged-in users still see them. Usernames are not a secret in WordPress, though. An author archive such as /?author=1 still redirects to /author/username/. Strong passwords, two-factor authentication and login rate limiting protect you; hiding usernames only removes an easy list.
Check that it works
Run these from a terminal, replacing the domain with yours:
# Should return 401 with the first snippet (404 with the second)
curl -i https://example.com/wp-json/wp/v2/users
# Should still return 200: the WooCommerce Store API
curl -i https://example.com/wp-json/wc/store/v1/cart
# Should return 200 for a signed-in request (application password)
curl -i -u 'username:xxxx xxxx xxxx xxxx xxxx xxxx' https://example.com/wp-json/wp/v2/usersCheck both URL styles: /wp-json/… and /?rest_route=/…. Both go through the same filter, so the result should match.
I tested both snippets in November 2026 on WordPress 7.1.4, WooCommerce 11.2.2 and PHP 8.4: guest requests to users, posts and the API index returned 401, the Store API cart and products routes returned 200, a look-alike route such as /wc/store-evil/ was still blocked and requests signed in with an application password were allowed. I didn’t test Contact Form 7 itself; its submissions use the /contact-form-7/v1/ route.
For more on hardening a site, see how I clean up a hacked WordPress site, or run a quick check with my WordPress security scanner. More snippets are on the code snippets page.
I used AI to help draft this post from my own idea and notes. I verified the information and edited the text myself.



