# Explain Why Visitors Were Sent to the Login Page

> Shows "Please log in to view that page" above the login form when a visitor was redirected there from a members-only page, instead of a bare form.

- Type: PHP
- Runs: Run everywhere
- Category: [Login](https://wpide.com/library/category/login/)
- Tags: login, message, members
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/login-notice-for-guests-on-protected-pages/

## Code

```php
add_filter( 'login_message', function ( $message ) {
    if ( empty( $_GET['redirect_to'] ) || isset( $_GET['action'] ) ) {
        return $message;
    }

    $target = wp_validate_redirect( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ), '' );

    if ( $target && false === strpos( $target, admin_url() ) ) {
        $message .= '<p class="message">Please log in to view that page.</p>';
    }

    return $message;
} );
```

## Notes

It shows whenever redirect_to points to a page on your own site rather than wp-admin.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Explain Why Visitors Were Sent to the Login Page" 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/
