[英]Wordpress: If user role is 'travel agent' redirect to 'my-account'
I created a custom role called "Travel Agent" using add_role( 'travel_agent', 'Travel Agent', array( 'book_hotel' ) );
我使用
add_role( 'travel_agent', 'Travel Agent', array( 'book_hotel' ) );
创建了一个自定义角色“ Travel Agent” add_role( 'travel_agent', 'Travel Agent', array( 'book_hotel' ) );
at my themes functions.php 在我的主题functions.php
However, I don't want this user to have access to the dashboard so I want to redirect him to "my-account" after log in. 但是,我不希望该用户访问仪表板,因此我想在登录后将其重定向到“我的帐户”。
I am using this code at wp-login.php/functions.php without any luck: 我在wp-login.php / functions.php上使用此代码,没有任何运气:
function redirect_agents() {
if ( current_user_can('book_hotel') ){
return '/my-account';
}
}
add_filter('login_redirect', 'redirect_agents');
How ever, I don't get redirected.. But if I use such code without the If at functions.php as this: 但是,我不会被重定向。。但是,如果我在功能.php中使用不带If的代码,如下所示:
function redirect_agents() {
return '/my-account';
}
add_filter('login_redirect', 'redirect_agents');
It works but then all users get redirected to my account. 它可以工作,但是所有用户都将重定向到我的帐户。 Any help is greatly appreciated!
任何帮助是极大的赞赏!
Why not use WordPress global $current_user. 为什么不使用WordPress全局$ current_user。
if(in_array('travel_agent', $current_user->roles)) {
return '/my-account';
}
The login_redirect
filter accepts three parameters: redirect_to
(contains current redirect value), request
(the URL the user is coming from) and user
(the user that has logged in as a WP_User object). login_redirect
过滤器接受三个参数: redirect_to
(包含当前重定向值), request
(用户来自的URL)和user
(作为WP_User对象登录的用户)。
You can use the user
parameter inside your function to determine whether to redirect or not: 您可以在函数内使用
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;
}
}
You might also want to verify that the $user
is a proper WP_User object, to ensure correct execution. 您可能还需要验证
$user
是正确的WP_User对象,以确保正确执行。
You can try using the $current_user global variable, but it may or may not be defined when login_redirect
is executed. 您可以尝试使用$ current_user全局变量,但是在执行
login_redirect
时可能会或可能不会定义它。
More information is available at the WordPress Codex . 有关更多信息,请访问WordPress Codex 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.