Your coming-soon page is up, so the site looks private. Open /wp-json/wp/v2/posts in a private window, though, and you may find every published post sitting there in full.
/wp-json/ keeps serving your content. And tell search engines to stay away, which on a single site is robots.txt plus “discourage search engines”, but on a subdirectory multisite subsite is only the second: the subsite’s robots.txt is never read. Logged-in editors and scripts that sign in with an application password keep working throughout. A complete must-use plugin with all three parts is free to copy at the end.Table of Contents
Check whether your coming-soon page leaks
Most “coming soon” and maintenance plugins work at the theme layer. A visitor asks for a page, the plugin answers with its own screen, and the theme never renders. That is all they promise, and for a human visitor it looks complete.
WordPress has a second front door, though. The REST API answers at /wp-json/ without involving the theme at all, and by default it serves published posts, pages and many custom post types to anyone, logged in or not. A wall built at the theme layer doesn’t stand in front of it.
Here is a test site behind a popular maintenance plugin, set to answer 503, requested without logging in:
GET / 503 (the maintenance page)
GET /wp-json/wp/v2/posts 200 full post bodies (5,630 characters for the first one)
GET /wp-json/wp/v2/product 200
The page said “come back later”. The API said “here is everything”. If your pre-launch site holds an unannounced product, a client’s draft copy or pricing you haven’t announced, this is the door to close first. To test your own, open https://example.com/wp-json/wp/v2/posts in a private browser window; a list of posts means the wall has a gap.
A coming-soon page closes the theme. The REST API was never behind it.
Three parts, three doors
A lockdown that actually holds has three parts, and each one closes a different door.
1. Every front-end request from a logged-out visitor goes to the login page. Hook template_redirect and call WordPress’s own auth_redirect(), which answers with a 302 to wp-login.php and remembers where the visitor was heading. Let two kinds of request through on purpose: admin-ajax.php, which the login screen and some plugins need, and wp-cron.php, which runs scheduled tasks such as backups. Feeds and sitemaps go through template_redirect too, so they are walled as well.
2. The REST API refuses anyone who is not logged in. The rest_authentication_errors filter returns a 401 for logged-out requests before any endpoint runs. Requests that carry an application password count as logged in, so deploy scripts and other automation keep working. A few plugins call back into the site from their own servers without logging in; leave a route open only for a service the site really uses (see the FAQ below).
3. Search engines are told to stay away. Replace robots.txt with Disallow: / and force the “discourage search engines” setting on, which adds a noindex to whatever a crawler can still reach.
With all three in place, a test network answered like this from outside, without logging in:
GET / 302 -> wp-login.php?redirect_to=...&reauth=1
GET /feed/ 302 -> wp-login.php
GET /wp-sitemap.xml 302 -> wp-login.php
GET /wp-json/wp/v2/posts 401 rest_not_logged_in
GET /wp-json/wp/v2/posts (app password) 200
GET /wp-admin/admin-ajax.php 400 (reachable; no action was sent)
GET /wp-cron.php 200
GET /wp-login.php 200 meta robots: noindex, nofollow, noarchive
| What a logged-out request reaches | Maintenance or coming-soon page | Three-part lockdown |
|---|---|---|
| Pages, feeds, sitemap | the maintenance screen (503 if set up right) | redirect to login (302) |
REST API (/wp-json/) | full content (200) | refused (401) |
| Automation with an application password | works | works |
| Login screen, admin-ajax, wp-cron | open | open, on purpose |
Why robots.txt does nothing on a multisite subsite
Part 3 deserves a closer look, because half of it depends on where the site lives.
On a single site, or the main site of a network, it works as written: on a test site, robots.txt answered User-agent: * / Disallow: / with the lockdown active. On a subdirectory subsite it does nothing for crawlers. Search engines read robots.txt only at the root of a host, so for a subsite at /shop/ they fetch the main site’s file and never ask for /shop/robots.txt. Requesting the subsite’s robots.txt directly got a 302 to the login page from the lockdown’s own redirect. The main site’s robots.txt, the one crawlers actually read, still disallowed only /wp-admin/.
In practice that is fine, because the subsite has two other defenses: every page redirects to a login screen that carries noindex, nofollow, noarchive, and the forced “discourage search engines” setting adds noindex wherever a crawler lands. Just don’t count robots.txt among them. If a subdirectory subsite must also be excluded by robots.txt, the rule has to go into the main site’s file. (The same subdirectory quirk shapes multisite permalinks: the network decides which site a path belongs to before that site’s own settings are ever read.)
On a subdirectory multisite, a subsite’s robots.txt is a file nobody reads.
Will the lockdown keep you out of search after launch?
The code forces “discourage search engines” with a filter rather than by changing the setting, and that raises a fair worry. Settings → Reading shows the box ticked while the filter is active, because the screen reads the filtered value. If an editor saved that screen for some unrelated reason, would the tick be written for real and keep a launched site out of search?
It is not. On a WordPress 7.0.2 test site we activated the lockdown, saved the Reading screen exactly as WordPress does (changing only posts per page), then removed the lockdown:
with the lockdown: setting reads 0 (stored value still 1); Reading box shown ticked
after a Reading save: stored value still 1
lockdown removed: setting 1; normal robots.txt; no noindex on pagesCode language: JavaScript (javascript)
WordPress compares the submitted value with the current one before writing, and both read as 0, so it skipped the write. Remove the file and the site goes back to whatever the setting really was. Launch-day checks that apply to any site still apply here: the status code your old wall returns (a maintenance page should answer 503), and the rest of the go-live audit.
Login wall or maintenance page?
They answer different questions, so pick by who should see the site:
- Nobody but your team yet (a build, a staging copy, a pre-launch subsite): use the login wall. Visitors get a 302 to the login screen, which tells crawlers nothing about your content, and the REST API stays shut.
- A live site that is briefly down (an upgrade, a migration): use a maintenance page that answers 503 with a Retry-After header, so search engines keep their index and come back later. If what’s on the site matters, add part 2 of this lockdown alongside it; the maintenance plugin alone won’t close the REST API.
Neither one is a bot filter. A login wall keeps out everyone who isn’t logged in, polite bots included. Telling good bots from bad ones on a live site is a different job, done at a different layer.
Free, no sign-up: the complete lockdown is below as a single must-use plugin file, MIT-licensed. Drop it into wp-content/mu-plugins/ and every part is on; delete the file at launch.
The complete lockdown, as a must-use plugin
A must-use plugin runs on every request and cannot be switched off from the Plugins screen by accident, which is what you want for a wall. On a multisite it applies to every site in the network. To lock only one subsite, run the same code through a snippet manager you can activate per site (choosing what runs network-wide).
<?php
/**
* Plugin Name: Pre-launch lockdown
* Description: Logged-out visitors are sent to the login page, the REST API refuses logged-out requests,
* and search engines are told to stay away. Delete this file at launch.
* License: MIT
*
* Tested on a WordPress multisite network and a WordPress 7.0.2 single site.
*/
// 1. Front end: send every logged-out visitor to the login page (feeds and sitemaps included).
add_action( 'template_redirect', function () {
if ( is_user_logged_in() ) {
return;
}
// Let the login screen's AJAX calls and scheduled tasks (backups, cron) through.
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) {
return;
}
auth_redirect(); // 302 to wp-login.php, remembering the page that was asked for
exit;
} );
// 2. REST API: refuse logged-out requests. Application passwords count as logged in.
add_filter( 'rest_authentication_errors', function ( $result ) {
if ( ! empty( $result ) ) {
return $result; // another check already decided
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'Restricted access: you are not logged in.', array( 'status' => 401 ) );
}
return $result;
} );
// 3. Search engines. robots.txt only works on a single site or a network's main site:
// crawlers never read a subdirectory subsite's robots.txt.
add_filter( 'robots_txt', function ( $output, $public ) {
return "User-agent: *\nDisallow: /";
}, 10, 2 );
// "Discourage search engines", forced on without changing the stored setting.
add_filter( 'pre_option_blog_public', '__return_zero' );
Code language: PHP (php)
The short version
- A coming-soon or maintenance page only covers the theme. On a test site the pages answered 503 while
/wp-json/answered 200 with full posts. - A real lockdown has three parts: redirect logged-out visitors to login, refuse logged-out REST requests with a 401, and keep search engines out.
- Automation still works. Requests with an application password count as logged in and got a 200 behind the wall.
- On a subdirectory multisite subsite, the robots.txt part does nothing. Crawlers read only the main site’s file; the login wall and the forced noindex do the work.
- Removing it at launch is clean. The “discourage search engines” override never touches the stored setting, even if someone saves the Reading screen while it is active.
Frequently Asked Questions
Does a coming-soon or maintenance plugin hide my posts from the REST API?
Usually not. Those plugins replace what the theme renders, and the REST API at /wp-json/ doesn’t go through the theme. On a test site behind a maintenance plugin, the pages answered 503 while /wp-json/wp/v2/posts returned every published post in full. Check yours by opening that address in a private window.
How do I block the WordPress REST API for logged-out users?
Return an error from the rest_authentication_errors filter when is_user_logged_in() is false, with a 401 status. WordPress then refuses the request before any endpoint runs. Pass through any error another plugin already set, as the code above does.
Will blocking the REST API for logged-out users break anything?
Logged-in editing keeps working, because the block editor sends its requests as you. Scripts that authenticate with an application password keep working too. What stops is anything that reads your REST API anonymously, such as a separate front end, a mobile app or an uptime check aimed at /wp-json/. On a site that is still private, that is the point.
Which REST API routes should stay open while the site is locked?
Only the routes of a service that calls your site from its own servers and is actually connected. A page cache with an online service is the usual case: LiteSpeed Cache’s QUIC.cloud services (image optimisation, critical CSS, CDN sync) call /wp-json/litespeed/ without logging in, so let that namespace through before you activate the plugin, or those calls get a 401. Jetpack’s server-to-server calls use the jetpack and wpcom namespaces. Check before you copy an exception from another site: WooCommerce registers jetpack/v4 by itself, with no Jetpack plugin installed, and a connection nobody set up needs no hole in the lock. Open /wp-json/ while logged in to see the namespaces the site registers, and keep only those whose plugin is active and connected.
Does robots.txt keep a multisite subsite out of search?
Not on a subdirectory network. Crawlers fetch robots.txt only from the root of the host, which is the main site’s file, so a rule a subsite adds to its own robots.txt is never read. Rely on the login redirect and noindex for the subsite, or add the rule to the main site’s robots.txt.
Should a pre-launch site redirect to login or return 503?
Redirect to login if nobody outside your team should see it yet; that keeps pages and the API private. Return 503 with a Retry-After header when a live site is briefly down, so search engines keep the pages they already indexed and come back later.
Is it safe to remove the lockdown at launch?
Yes. The code forces “discourage search engines” with a filter, not by changing the setting. In a test, saving the Reading screen while it was active left the stored value untouched, and once the file was gone the site’s robots.txt and pages were back to normal.