Php WordPress基于角色的登录重定向不起作用
我按照在线说明在我的子主题的Php WordPress基于角色的登录重定向不起作用,php,wordpress,Php,Wordpress,我按照在线说明在我的子主题的functions.php文件中添加了一个函数,它应该将某些角色的用户重定向到特定的URL 不幸的是,这不起作用 我已经彻底地重新分析了我的代码,并将其与各种在线解决方案进行了比较,但我无法让它对我的网站产生任何影响 我的代码: // Custom successful login redirect based on user role function my_login_redirect( $redirect_to, $request, $user ) {
functions.php
文件中添加了一个函数,它应该将某些角色的用户重定向到特定的URL
不幸的是,这不起作用
我已经彻底地重新分析了我的代码,并将其与各种在线解决方案进行了比较,但我无法让它对我的网站产生任何影响
我的代码:
// Custom successful login redirect based on user role
function my_login_redirect( $redirect_to, $request, $user ) {
// is there a user to check?
global $user;
if (isset($user->roles) && is_array($user->roles)) {
// check for 'company' user role and redirect in case
if (in_array('company', $user->roles)) {
$redirect_to = home_url('/arbeitskraft-finden/');
// check for 'worker' user role and redirect in case
} elseif (in_array('worker', $user->roles)) {
$redirect_to = home_url('/wp-admin/chat/');
// otherwise, redirect to wp-admin
} else {
$redirect_to = home_url('/wp-admin/');
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
// END Custom successful login redirect based on user role
预期成果:
- 角色为“company”的用户将被重定向到/arbeitskraft finden/
- 角色为“worker”的用户将被重定向到/wp admin/chat/
- 具有其他角色的用户将被重定向到/wp admin/
// Custom successful login redirect based on user role
function my_login_redirect( $redirect_to, $request, $user ) {
// is there a user to check?
global $user;
if (isset($user->roles) && is_array($user->roles)) {
// check for 'company' user role and redirect in case
if (in_array('company', $user->roles)) {
$redirect_to = home_url('/arbeitskraft-finden/');
// check for 'worker' user role and redirect in case
} elseif (in_array('worker', $user->roles)) {
$redirect_to = home_url('/wp-admin/chat/');
// otherwise, redirect to wp-admin
} else {
$redirect_to = home_url('/wp-admin/');
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
// END Custom successful login redirect based on user role
我回到了我使用的最初信息来源,WordPress codex: 我意识到变量
$user
不是在函数中全局定义的。我不知道我的代码里怎么会有这个
在删除它之后,以及在删除第一个if
语句的第二个else
之后,该代码将正常工作,如下所示:
// Custom successful login redirect based on user role
function my_login_redirect( $redirect_to, $request, $user ) {
// is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
// check for 'company' user role and redirect in case
if (in_array('company', $user->roles)) {
$redirect_to = home_url('/arbeitskraft-finden/');
// check for 'worker' user role and redirect in case
} elseif (in_array('worker', $user->roles)) {
$redirect_to = home_url('/wp-admin/chat/');
// otherwise, redirect to wp-admin
} else {
$redirect_to = home_url('/wp-admin/');
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
// END Custom successful login redirect based on user role
尝试以下操作:,然后在
global$user之后添加以下内容代码>:错误日志(打印($user->roles,true))
并尝试再次登录。然后检查debug.log
文件的内容,它说了什么?您是否尝试过使用Xdebug,它可能会帮助您快速发现问题,我建议您这样做。因此,您可以使用断点并了解代码中发生了什么。感谢您的后期编辑和推荐的解决方案@cabrerahector。我按照你的建议修改了文件。但是,当我登录时,/wp config/
中没有出现debug.log文件。我尝试了多次(登录)。好像我的代码没有执行?然而,在相同的functions.php
中的所有其他功能似乎都可以工作。还有其他想法吗@莫普林也谢谢你的建议。目前,我没有使用复杂的本地开发环境。到目前为止,我还没有使用xdebug,在阅读了一个小时左右之后,我认为这是目前最后的选择。debug.log文件应该位于/wp content
(这是打字错误吗?)尝试手动创建该文件,然后重复上面的步骤。如果文件是空的,那么我会检查您使用的过滤器挂钩是否正确。@cabrerahector这确实是一种类型。手动创建日志文件后,仍然没有记录任何内容。但是,我找到了问题的解决方案,我将用代码添加一个新的答案。谢谢你的帮助!感谢你的帮助@initipientfo我尝试过这个代码,但它没有用。但是,我找到了解决方案,并将在新的答案中发布。