# Require Login to View the Site

> Makes the whole front end private: visitors who are not logged in are sent to the login page and returned to the page they asked for afterwards.

- Type: PHP
- Runs: Frontend only
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: redirects, private, login, intranet
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/members-only-site/

## Code

```php
add_action( 'template_redirect', function () {
    if ( is_user_logged_in() ) {
        return;
    }

    // Page slugs that stay public, for example array( 'contact', 'privacy-policy' ).
    $public_pages = array();

    if ( $public_pages && is_page( $public_pages ) ) {
        return;
    }

    auth_redirect();
} );
```

## Notes

Feeds and the REST API are not covered. Combine with "Restrict the REST API to Logged-In Users" for full privacy.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require Login to View the Site" 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/
