auth_redirect()
Checks if a user is logged in, if not it redirects them to the login page.
When this code is called from a page, it checks to see if the user viewing the page is logged in. If the user is not logged in, they are redirected to the login page. The user is redirected in such a way that, upon logging in, they will be sent directly to the page they were originally trying to access.
Pluggable function — this function can be replaced from a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.
Function replacement (override) — in must-use or regular plugin you can create a function with the same name, then it will replace this function.
Hooks from the function
Return
null
. Nothing (null).
Usage
auth_redirect();
Examples
#1 Close content from unauthorized users and redirect them to login
Require a user to log in in order to view site:
if ( ! is_user_logged_in() ) { auth_redirect(); }
Close posts from unauthorized users:
if ( !is_user_logged_in() && is_single() ) { auth_redirect(); }
Use this code at the beginning of header.php.
Changelog
Since 1.5.0 | Introduced. |