![](/img/trans.png)
[英]How can I make my WordPress site redirect to subdomain once successfully login they will redirect back to root domain
[英]How can I make login redirects from a URL on my Wordpress site?
我在函数中的重定向 function.php 有这个结构——
//After Login Redirection
function my_login_redirect( $redirect_to, $request, $user ) {
$user_id = (isset($user->ID) ? $user->ID : false);
$urlparameters = $_SERVER['QUERY_STRING'];
$decode = urldecode($urlparamiters);
preg_match('~=(.*?)&~',$decode, $redirecturl);
//If the paramiter redirect_to is found in the url then run the url redirect.
if(strpos($urlparameters, "redirect_to")!== false) {
return $redirecturl[1];
} else {
//If no redirect in the URL then use standard redirect according to roles
if (isset($user->roles) && is_array($user->roles)) {
if (in_array('administrator', $user->roles)) {
//is this the administrator?
$redirect_to = home_url( $path = '/wordpress/wp-admin/', $scheme = 'https' );
}
elseif (in_array('client', $user->roles)) {
// Take clients to the private messaging board
$redirect_to = home_url( $path = '/wordpress/pm/', $scheme = 'https' );
}
}
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
我想要什么,我得到什么
当客户访问我的网站时,要么直接从 wp-login.php 页面访问,要么因为他们已被发送到特定页面。 如果发送到特定页面,url 将包含登录重定向参数。 如果 url 有一个以“redirect_to”开头的参数,我只想运行该重定向。 如果 URL 中没有参数,我想运行我的标准重定向序列。 上面的代码不起作用。 它只运行我的标准重定向例程。 所以,我的第一个 IF 条件不起作用,我不知道为什么。
我尝试过的和看到的
当我登陆登录页面时,如果我回显 $urlparameters 我可以看到它们被准确捕获。 如果我回显 $decode 和 $redirecturl[1] 我会看到我期望的值。 然而,我点击了登录按钮,function 运行起来就好像 $urlparameters、$decode 和 $redirecturl[1] 变量为空一样。
如果我使用重定向 URL 并删除第二个 IF 条件(禁用使用标准重定向的能力),客户将被定向到他们的 WordPress 配置文件屏幕。 所以就好像 $redirecturl[1] 只是空的。 如果我使 $redirecturl[1] 发生相同的重定向的唯一选项(无条件)。 如果我保留第二个 IF 条件,则代码运行就像 URL 中没有参数一样,并且标准重定向已完成。
错误日志产生 Undefined offset: 1 for $redirecturl[1] 表明它是空的。 然而,当我回显变量时,我看到了一个值。
请帮助我理解以下内容:
为什么我的变量($urlparameters、$decode 和 $redirecturl[1])回显了预期值,但是当我点击登录按钮时,它们的功能就好像它们为空一样。 我该如何纠正?
我是否因为点击了登录按钮而丢失了 session 数据? 如果是这样,我怎样才能将数据保持到下一个屏幕?
更新代码:
//After Login Redirection
function my_login_redirect( $redirect_to, $request, $user ) {
$user_id = (isset($user->ID) ? $user->ID : false);
$urlparameters = $_SERVER['QUERY_STRING'];
$decode = urldecode($urlparamiters);
preg_match('~=(.*?)&~',$decode, $redirecturl);
setcookie("urlparamiters", $urlparamiters, time()+120);
setcookie("redirecturl", $redirecturl[1], time()+120);
//If the paramiter redirect_to is found in the url then run the url redirect.
if(strpos($_COOKIE["urlparamiters"], "redirect_to") !== false){
return $_COOKIE["redirecturl"];
} else {
//If no redirect in the URL then use standard redirect according to roles
if (isset($user->roles) && is_array($user->roles)) {
if (in_array('administrator', $user->roles)) {
//is this the administrator?
$redirect_to = home_url( $path = '/wordpress/wp-admin/', $scheme = 'https' );
}
elseif (in_array('client', $user->roles)) {
// Take clients to the private messaging board
$redirect_to = home_url( $path = '/wordpress/pm/', $scheme = 'https' );
}
}
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
我发现按下登录按钮时我丢失了数据,因为它没有维护 session。 所以,我的解决方案是使用 cookies 来管理这个。 它现在按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.