auth_redirect()
Checks if the user is authorized before allowing them on any page of the site.
If the user is not authorized, it redirects them to the login page.
Needed to restrict pages from unregistered users.
If this function is called on the posts page and the user is not registered and not authorized (not logged in with their username), they will be redirected to the login page. After logging in, the user will be returned to the page they were on before the redirection.
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
Returns
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. |