By default, WordPress allows an unauthenticated user to view the list of the registered users with the help of the REST API. But, for example, to view the list of the users in the Dashboard, the user needs to have list_users capability (that is, be an Administrator). While the REST API does not expose sensitive information (such as emails) to unauthenticated users, it may be desirable to restrict users endpoint form unauthenticated users.

Fortunately, this is easy.

The first if checks the request method and the endpoint called, the second if checks whether the user has necessary capabilities. You may want to customize these checks to suite your needs. By returning WP_Error, we effectively stop the further processing of the request, and return this error to the caller.

Enjoy! 🙂

Useful links:

How to Restrict Access to User REST API in WordPress
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *