Description
This function redirects a user who is not logged in to the login page (if one is set in the main options tab).
Parameters
$redirect_to
(string) (optional) URL to redirect back to following login. (default: false)
Usage
[snippet slug=wpmem_redirect_to_login lang=php]
Examples
Redirect all content that is not the login page, register page, or user profile (for forgot password link) to the login page if the user is not logged in:
[snippet slug=redirect-all-pages-to-login lang=php]
Redirect a specific page to the login page:
[snippet slug=redirect-blog-page-to-login lang=php]
Redirect blocked content to login:
[snippet slug=redirect-blocked-content-to-login lang=php]
Notes
- The function automatically applies a redirect back to the requested page. You only need to pass a redirect_to argument in the function if you want to redirect to a URL that is not the requested URL.
- This function can be helpful in situations where you may have something to block that is not handled the same way as ordinary content. Some examples would be custom post types that display content outside of the main $content variable (such as event calendars, forums, etc).
Changelog
- Introduced in version 3.0.2
- Moved to API in 3.1.1
- Added redirect_to argument in 3.1.3
Source
wpmem_redirect_to_login() is located in /includes/api/api.php.