Php Wordpress:如果用户角色为';旅行社';重定向到';我的账户';
我使用Php Wordpress:如果用户角色为';旅行社';重定向到';我的账户';,php,wordpress,Php,Wordpress,我使用add_-role('Travel_-Agent','Travel-Agent',array('book_-hotel'))创建了一个名为“旅行社”的自定义角色在my themes functions.php 但是,我不希望此用户能够访问仪表板,因此我希望在登录后将其重定向到“我的帐户” 我在wp-login.php/functions.php使用此代码时没有任何运气: function redirect_agents() { if ( current_user_can('book_h
add_-role('Travel_-Agent','Travel-Agent',array('book_-hotel'))创建了一个名为“旅行社”的自定义角色代码>在my themes functions.php
但是,我不希望此用户能够访问仪表板,因此我希望在登录后将其重定向到“我的帐户”
我在wp-login.php/functions.php使用此代码时没有任何运气:
function redirect_agents() {
if ( current_user_can('book_hotel') ){
return '/my-account';
}
}
add_filter('login_redirect', 'redirect_agents');
然而,我没有被重定向。。但是如果我在functions.php中使用这样的代码而不使用if,则如下所示:
function redirect_agents() {
return '/my-account';
}
add_filter('login_redirect', 'redirect_agents');
它可以工作,但所有用户都会被重定向到我的帐户。非常感谢您的帮助 为什么不使用WordPress global$current\u用户
if(in_array('travel_agent', $current_user->roles)) {
return '/my-account';
}
login\u redirect
过滤器接受三个参数:redirect\u to
(包含当前重定向值)、request
(用户来自的URL)和user
(作为WP\u用户对象登录的用户)
您可以在函数中使用user
参数来确定是否重定向:
add_filter( "login_redirect", "custom_login_redirect", 10, 3 );
function custom_login_redirect( $redirect_to, $request, $user )
{
if ( in_array( "role_name", $user -> roles ) )
{
return "/hello-world";
}
// Remember to return something just in case,
// as filters can possibly block execution if
// they do not return anything.
else
{
return $redirect_to;
}
}
您可能还需要验证$user
是否是正确的WP\u用户对象,以确保正确执行
您可以尝试使用$current_user全局变量,但在执行login_redirect
时可能会定义它,也可能不会定义它
.是的,酒店是定制的,http://codex.wordpress.org/Function_Reference/add_cap
您可以注册自己的功能。唯一的问题是,这意味着他们在登录时将无法查看任何其他页面。我猜这将包含在函数中,并在其他位置有条件地调用。是的,对不起,这是有意义的!