# Members-Only Subsites

> Makes the subsites you list visible only to logged-in members of that site; everyone else is sent to the login page and non-members see a friendly notice.

- Type: PHP
- Runs: Run everywhere
- Category: [Multisite](https://wpide.com/library/category/multisite/)
- Tags: private, members, access
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/ms-private-subsites/

## Code

```php
if ( ! is_multisite() ) {
    return;
}

$wpide_private_sites = array(); // Blog IDs that only members can view, e.g. array( 3, 7 ).

if ( ! in_array( get_current_blog_id(), $wpide_private_sites, true ) ) {
    return;
}

add_action( 'template_redirect', function () {
    if ( is_user_logged_in() && ( is_user_member_of_blog() || is_super_admin() ) ) {
        return;
    }

    if ( ! is_user_logged_in() ) {
        auth_redirect();
    }

    wp_die( esc_html__( 'This site is only open to its members.' ), esc_html__( 'Members only' ), array( 'response' => 403 ) );
}, 1 );

add_filter( 'rest_authentication_errors', function ( $result ) {
    if ( ! empty( $result ) || ( is_user_logged_in() && ( is_user_member_of_blog() || is_super_admin() ) ) ) {
        return $result;
    }

    return new WP_Error( 'wpide_members_only', __( 'This site is only open to its members.' ), array( 'status' => 401 ) );
} );
```

## Notes

List blog IDs in $wpide_private_sites. Feeds and the REST API are covered too; super admins always have access.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Members-Only Subsites" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
