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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.