login_redirect
Allows you to change the URL to which a user is redirected after logging in.
Usage
add_filter( 'login_redirect', 'wp_kama_login_redirect_filter', 10, 3 );
/**
* Function for `login_redirect` filter-hook.
*
* @param string $redirect_to The redirect destination URL.
* @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
* @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
*
* @return string
*/
function wp_kama_login_redirect_filter( $redirect_to, $requested_redirect_to, $user ){
// filter...
return $redirect_to;
}
- $redirect_to(string)
- The URL WordPress determined for redirecting the user.
- $requested_redirect_to(string)
- The original raw redirect URL from
$_REQUEST['redirect_to']. WordPress used it to obtain$redirect_to. - $user(WP_User/WP_Error)
- The current user (a WP_User object) if authentication succeeded. Otherwise, a WP_Error object.
Examples
#1 Redirect to the home page after login
In this example, administrators are redirected to the default page and other users to the home page.
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
/**
* Redirect user after successful login.
*
* @param string $redirect_to URL to redirect to.
* @param string $request URL the user is coming from.
* @param object $user Logged user's data.
* @return string
*/
function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
// check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
}
else {
return home_url();
}
}
else {
return $redirect_to;
}
}Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
In file: /wp-login.php
login_redirect
wp-login.php 1353
$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );