{"id":46928,"date":"2026-10-24T08:04:02","date_gmt":"2026-10-24T08:04:02","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46928"},"modified":"2026-10-24T08:04:02","modified_gmt":"2026-10-24T08:04:02","slug":"wordpress-rest-api-restricted-authenticated-users","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-rest-api-restricted-authenticated-users\/","title":{"rendered":"WordPress REST API Restricted to Authenticated Users"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">By default, anyone can read the WordPress REST API without logging in. A request to <code>\/wp-json\/wp\/v2\/users<\/code> returns the slugs of everyone who has published a post, which is often their login name. <code>\/wp-json\/wp\/v2\/posts<\/code> and <code>\/wp-json\/wp\/v2\/pages<\/code> return your content, custom fields that are registered with <code>show_in_rest<\/code>, and draft-free metadata to anyone who asks. If your site doesn&#8217;t run a headless front end, nobody outside your team needs that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet restricts the WordPress REST API to authenticated users. Logged-out requests get a clean <code>401<\/code> JSON error. The block editor, application passwords and anything else that authenticates properly keep working. You also get an allowlist for the few routes that guests really do need, such as oEmbed or a contact form endpoint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put this in a small plugin, an mu-plugin, your theme&#8217;s functions.php, or a Scripts Organizer PHP block set to run everywhere.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/**\n * Restrict the WordPress REST API to authenticated users,\n * with an allowlist of routes that stay public.\n *\/\n\nfunction dpc_rest_public_routes(): array {\n\t$routes = &#91;\n\t\t'\/oembed\/1.0', \/\/ Lets other sites embed your posts.\n\t&#93;;\n\n\t\/**\n\t * Route prefixes that logged-out visitors may still call.\n\t *\n\t * @param string&#91;&#93; $routes e.g. '\/oembed\/1.0', '\/wc\/store', '\/contact-form-7\/v1'.\n\t *\/\n\treturn (array) apply_filters( 'dpc_rest_public_routes', $routes );\n}\n\nfunction dpc_rest_route_is_public( string $route ): bool {\n\t$route = '\/' . trim( $route, '\/' );\n\n\tforeach ( dpc_rest_public_routes() as $prefix ) {\n\t\t$prefix = '\/' . trim( (string) $prefix, '\/' );\n\n\t\tif ( $route === $prefix || str_starts_with( $route, $prefix . '\/' ) ) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nadd_filter( 'rest_pre_dispatch', 'dpc_rest_require_login', 10, 3 );\n\nfunction dpc_rest_require_login( $result, $server, $request ) {\n\t\/\/ Another filter already answered, or the request is authenticated.\n\tif ( null !== $result || is_user_logged_in() ) {\n\t\treturn $result;\n\t}\n\n\tif ( dpc_rest_route_is_public( $request-&gt;get_route() ) ) {\n\t\treturn $result;\n\t}\n\n\treturn new WP_Error(\n\t\t'dpc_rest_login_required',\n\t\t__( 'The REST API on this site is only available to logged-in users.', 'dpc' ),\n\t\t&#91; 'status' =&gt; rest_authorization_required_code() &#93;\n\t);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rest_pre_dispatch<\/code> runs before WordPress hands any request to a route&#8217;s callback, and it gets the full <code>WP_REST_Request<\/code>. That lets the snippet check the route, which the more common <code>rest_authentication_errors<\/code> filter can&#8217;t see without parsing the URL itself.<\/li>\n\n\n<li>Authentication has already happened by this point. Cookie logins with a valid nonce, application passwords and any auth plugin have set the current user, so <code>is_user_logged_in()<\/code> gives the real answer.<\/li>\n\n\n<li>If the user isn&#8217;t logged in and the route doesn&#8217;t match an allowlisted prefix, the snippet returns a <code>WP_Error<\/code>. <code>rest_authorization_required_code()<\/code> turns that into a <code>401<\/code> for logged-out visitors.<\/li>\n\n\n<li>Matching uses whole path segments. <code>\/wc\/store<\/code> allows <code>\/wc\/store\/v1\/cart<\/code> but does not allow <code>\/wc\/storefront<\/code>.<\/li>\n\n\n<li>Route callbacks never run, so a blocked request costs almost nothing.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the routes your front end calls as a guest. Use the namespace as the prefix:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'dpc_rest_public_routes', function ( array $routes ): array {\n\t$routes&#91;&#93; = '\/wc\/store';          \/\/ WooCommerce cart and checkout blocks.\n\t$routes&#91;&#93; = '\/contact-form-7\/v1'; \/\/ Contact Form 7 submissions.\n\treturn $routes;\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To allow only some roles instead of every logged-in user, replace <code>is_user_logged_in()<\/code> with a capability check such as <code>current_user_can( 'edit_posts' )<\/code>. Subscribers and customers will then get a <code>403<\/code> instead, because <code>rest_authorization_required_code()<\/code> returns 403 for users who are logged in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Guest-facing blocks break silently.<\/strong> The WooCommerce cart and checkout blocks, many form plugins, search-as-you-type widgets and some analytics plugins call the REST API from the browser for logged-out visitors. Open your key pages in a private window, check the browser&#8217;s Network tab for <code>401<\/code> responses, and allowlist those namespaces.<\/li>\n\n\n<li><strong>Your own JavaScript needs a nonce.<\/strong> If a logged-in user&#8217;s request has no <code>X-WP-Nonce<\/code> header (or no <code>_wpnonce<\/code> parameter), core treats it as logged out on purpose, to prevent CSRF, so this snippet blocks it. Send a nonce created with <code>wp_create_nonce( 'wp_rest' )<\/code>, or use <code>wp.apiFetch<\/code>, which adds one for you.<\/li>\n\n\n<li><strong>Headless or app front ends<\/strong> have to authenticate, for example with application passwords over HTTPS, or you have to allowlist the routes they read.<\/li>\n\n\n<li>The <code>\/wp-json\/<\/code> discovery link in the page head stays. It reveals nothing once the routes return 401.<\/li>\n\n\n<li>Server-side code that calls <code>rest_do_request()<\/code> while nobody is logged in, such as a cron job, is blocked too. Allowlist that route or switch to a user with <code>wp_set_current_user()<\/code> for that job.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For more hardening along the same lines, <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/disable-xml-rpc-wordpress-without-plugin\/\">disable XML-RPC<\/a>, which is the other public API many sites never use, and consider <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/lockdown-admin-backend-to-your-ip\/\">restricting wp-admin and login to your IP address<\/a>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Restrict the WordPress REST API to logged-in users with one filter. Keep an allowlist of public routes such as oEmbed or the WooCommerce Store API.<\/p>\n","protected":false},"author":1,"featured_media":46929,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress REST API Restricted to Authenticated Users","description":"Restrict the WordPress REST API to logged-in users with one filter. Keep an allowlist of public routes such as oEmbed or the WooCommerce Store API."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[226,224,161,144,150],"dpc_library":[],"class_list":["post-46928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-authentication","tag-hooks","tag-rest-api","tag-security","tag-users"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46928"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46928\/revisions"}],"predecessor-version":[{"id":46930,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46928\/revisions\/46930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46929"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46928"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46928"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}