login_redirect
云策文档标注
概述
login_redirect 过滤器用于在用户登录后修改重定向URL,允许开发者基于用户角色或条件自定义跳转目标。文档提供了参数说明、使用注意事项和代码示例。
关键要点
- 过滤器名称:login_redirect,用于修改登录后的重定向URL。
- 参数:$redirect_to(当前重定向URL)、$requested_redirect_to(请求的重定向URL)、$user(WP_User 或 WP_Error 对象)。
- 注意事项:避免使用 $current_user 全局变量,应使用 $user 参数;使用 add_filter 时需确保函数名唯一,且 is_admin() 可能不可用。
- 版本历史:自 WordPress 3.0.0 引入。
代码示例
function my_login_redirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return $redirect_to;
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );注意事项
- 避免使用 $user->has_cap() 检查角色,可能导致不可靠结果;推荐直接检查 $user->roles 数组。
- 注册过滤器时需指定所有三个参数,如 add_filter( 'login_redirect', 'function_name', 10, 3 )。
原文内容
Filters the login redirect URL.
Parameters
Source
$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user );
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 5 content
Niels Lange
Examples
This example redirects admins to the dashboard and other users to the homepage. Make sure you use add_filter outside of is_admin() , since that function is not available when the filter is called.
/** * 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; } } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );Skip to note 6 content
Niels Lange
Notes
You can register the login_redirect filter to use all 3 parameters like this:
In the example, ‘filter_function_name’ is the function WordPress should call during the login process. Note that filter_function_name should be unique function name. It cannot match any other function name already declared.
The $current_user global may not be available at the time this filter is run. So you should use the $user global or the $user parameter passed to this filter.
Skip to note 7 content
Steven Lin
Example Migrated from Codex:
Redirect all logins to the homepage with an anonymous function (php 5.3+).
add_filter( 'login_redirect', function( $url, $query, $user ) { return home_url(); }, 10, 3 );Skip to note 8 content
Dhimas Kirana
has_cap( 'administrator' ) ) { $url = admin_url(); } else { $url = home_url( '/members-only/' ); } } return $url; } add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );Thanks WP Scholar : https://wpscholar.com/blog/wordpress-user-login-redirect/ 😀
$user->has_cap()to check a role is discouraged as it may produce unreliable results.