尝试重定向到用户配置文件
Attempting to Redirect to User Profile
我试图使用 BuddyPress 将所有注册用户重定向到他们的用户个人资料;然而,当重定向结果 URL 不包括 user_login... 结果每个用户都重定向到 "domain/members/".
如何让用户名显示在 URL 中,然后重定向到以下内容:"domain/members/username/"?
FUNCTIONS.PHP
add_filter('login_redirect', function ($redirect_to, $request, $user){
if (user_can($user, 'registered')){
$current_user = wp_get_current_user();
return '/members/' . $current_user->user_login . '/';
}
}
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
重定向函数将此函数调用为 redirect_to("redirect_page");
如 WordPress 法典所述:"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."
https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
add_filter('login_redirect', function ($redirect_to, $request, $user){
if (user_can($user, 'registered')){
return bp_core_get_user_domain( $user->ID );
}
}
不依赖于硬编码 slug 的更好方法:
return bp_core_get_user_domain( $user->ID );
我试图使用 BuddyPress 将所有注册用户重定向到他们的用户个人资料;然而,当重定向结果 URL 不包括 user_login... 结果每个用户都重定向到 "domain/members/".
如何让用户名显示在 URL 中,然后重定向到以下内容:"domain/members/username/"?
FUNCTIONS.PHP
add_filter('login_redirect', function ($redirect_to, $request, $user){
if (user_can($user, 'registered')){
$current_user = wp_get_current_user();
return '/members/' . $current_user->user_login . '/';
}
}
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
重定向函数将此函数调用为 redirect_to("redirect_page");
如 WordPress 法典所述:"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."
https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
add_filter('login_redirect', function ($redirect_to, $request, $user){
if (user_can($user, 'registered')){
return bp_core_get_user_domain( $user->ID );
}
}
不依赖于硬编码 slug 的更好方法:
return bp_core_get_user_domain( $user->ID );