[英]Redirecting user to url with username wordpress
I want to redirect users after login based on username. 我想在登录后根据用户名重定向用户。
add_filter( 'login_redirect', 'wppbc_custom_login_redirect', 99, 3 );
function wppbc_custom_login_redirect( $redirect_to, $requested_redirect_to,
$user ) {
if( is_wp_error( $user ) ) {
return $redirect_to;
}
$username = $user->user_login;
if ( is_array( $form_args ) ) {
switch ( $username ) {
case 'User1':
$redirect_to = 'https://www.example.com/User1';
break;
case 'User2':
$redirect_to = 'https://www.example.com/User2';
break;
default:
$redirect_to = 'https://www.example.com';
}
}
return $redirect_to;
}
The only problem is that no i have to manually add every user to this list with the domain i want them to be redirected to after a succesfull login. 唯一的问题是,没有我必须手动将每个用户添加到该列表,并且我希望他们在成功登录后被重定向到该域。 I want a single code where i can add the Username variable to the domain. 我想要一个可以在其中将Username变量添加到域的代码。
User name x logs in succesfully and will be redirected to www.example.com/x User name y logs in succesfully and will be redirected to www.example.com/y 用户名x成功登录,并将重定向到www.example.com/x用户名y成功登录,并将重定向到www.example.com/y
Can anyone help me with this code? 谁能帮我这个代码?
You can not do like this have to post back the javascript variable to your server before the server can handle the value. 您不能这样做,必须将javascript变量发回服务器,然后服务器才能处理该值。 To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. 为此,您可以编写提交表单的JavaScript函数,也可以使用ajax / jquery。 jQuery.post jQuery.post
Maybe the most easiest approach for you is something like this 也许对您来说最简单的方法是这样的
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name'] after your javascript was executed. 在执行JavaScript后,您可以在myphpfile.php上使用$ _GET ['name']。
Ok i don't understand a clue of javascript haha. 好的,我不了解JavaScript的提示。 The redirect 重定向
$redirect_to = 'https://www.example.com/' . $username;
Doesn't work.. 不起作用..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.