Php Wordpress-按用户角色限制页面-URL重定向
我试图限制除“图书管理员”之外的所有用户角色的页面 我在Php Wordpress-按用户角色限制页面-URL重定向,php,wordpress,function,if-statement,redirect,Php,Wordpress,Function,If Statement,Redirect,我试图限制除“图书管理员”之外的所有用户角色的页面 我在example.com/library dashboard 当非“图书管理员”的登录用户角色访问此页面时,我需要将其重定向到example.com/subscription needed 我正在为此使用以下功能: function is_corr_user($page_slug) { // User has to be logged in if(!is_user_logged_in()) return false;
example.com/library dashboard
当非“图书管理员”的登录用户角色访问此页面时,我需要将其重定向到example.com/subscription needed
我正在为此使用以下功能:
function is_corr_user($page_slug) {
// User has to be logged in
if(!is_user_logged_in())
return false;
// All user roles
$roles = wp_get_current_user()->roles;
// For each page check if user has required role
switch($page_slug) {
case "library-dashboard":
return in_array('librarian, administrator', $roles);
default:
return false;
}
}
// Hook to wordpress before load and check if correct user is on page
add_action( 'wp', 'wpse69369_is_correct_user' );
function wpse69369_is_correct_user()
{
global $post;
// Redirect to a custom page if wrong user
if(!is_corr_user($post->post_name)) {
wp_redirect( '/subscription-needed/' );
exit;
}
}
我的问题是,这个函数现在将所有页面重定向到example.com/subscription needed/
,包括主页,我收到了太多的重定向错误
如何解决此问题,使函数仅适用于example.com/library dashboard
页面上的给定用户角色library
因此,我试图实现的是,如果图书管理员
和管理员
访问example.com/library dashboard
,那么什么都不会发生,页面显示正常
但是,如果不是图书管理员
和管理员
的任何其他用户角色访问了example.com/library dashboard
,则应将其重定向到example.com/subscription needed/
以下代码进行检查
add_action('wp', 'redirectUserOnrole');
function redirectUserOnrole() {
//First i am checking user logged in or not
if (is_user_logged_in()) {
$user = wp_get_current_user();
$role = (array) $user->roles;
//checking for the user role you need to change the role only if you wish
if ($role[0] != 'librarian' || $role[0] != 'administrator') {
global $post;
if ($post->post_name == 'library-dashboard') {
wp_redirect('/subscription-needed/');
exit;
}
}
} else {
return true;
}
}
检查以下代码
add_action('wp', 'redirectUserOnrole');
function redirectUserOnrole() {
//First i am checking user logged in or not
if (is_user_logged_in()) {
$user = wp_get_current_user();
$role = (array) $user->roles;
//checking for the user role you need to change the role only if you wish
if ($role[0] != 'librarian' || $role[0] != 'administrator') {
global $post;
if ($post->post_name == 'library-dashboard') {
wp_redirect('/subscription-needed/');
exit;
}
}
} else {
return true;
}
}
这对我很有用,您可以使用它来代替
is_corr_user()
和wpse69369_is_correct_user()
函数:
add_action( 'template_redirect', 'librarian_dashboard_redirect' );
function librarian_dashboard_redirect() {
if ( is_user_logged_in() && is_page( 'library-dashboard' ) ) {
$user = wp_get_current_user();
$valid_roles = [ 'administrator', 'librarian' ];
$the_roles = array_intersect( $valid_roles, $user->roles );
// The current user does not have any of the 'valid' roles.
if ( empty( $the_roles ) ) {
wp_redirect( home_url( '/subscription-needed/' ) );
exit;
}
}
}
这对我很有用,您可以使用它来代替
is_corr_user()
和wpse69369_is_correct_user()
函数:
add_action( 'template_redirect', 'librarian_dashboard_redirect' );
function librarian_dashboard_redirect() {
if ( is_user_logged_in() && is_page( 'library-dashboard' ) ) {
$user = wp_get_current_user();
$valid_roles = [ 'administrator', 'librarian' ];
$the_roles = array_intersect( $valid_roles, $user->roles );
// The current user does not have any of the 'valid' roles.
if ( empty( $the_roles ) ) {
wp_redirect( home_url( '/subscription-needed/' ) );
exit;
}
}
}
我认为在第28行/第26行中,你应该使用
is\u corr\u user()
而不是is\u correct\u user()
。你完全正确!致命错误消失了,但又出现了另一个问题。出于某种原因,我的主页现在正在重定向到/subscription needed/
页面。为什么会这样?是的,这就是它的工作原理为什么因为您没有指定任何特定页面,因此它适用于所有页面,包括homepage@JoeBloggs请编辑您的问题,因为您找到了此问题的解决方案(致命错误问题),但似乎您提出了不同的问题。我认为在第28行/第26行中,您应该使用is#corr#u user()
而不是是正确的用户()
。你完全正确!致命错误消失了,但又出现了另一个问题。出于某种原因,我的主页现在正在重定向到/subscription needed/
页面。为什么会这样?是的,这就是它的工作原理为什么因为您没有指定任何特定页面,因此它适用于所有页面,包括homepage@JoeBloggs请编辑您的问题,因为您找到了此问题的解决方案(致命错误问题),但似乎您提出了不同的问题Hey Akshay,我认为您的问题正好相反。。我现在被转为图书管理员。。应该是这样的:如果用户角色不是图书管理员
或不是管理员
,则重定向到/subscription needed/
,但如果用户是图书管理员或是管理员
,则重定向到,然后停留在当前的库仪表板上
页面确定您可以更改我的if条件,我相信您会得到结果。代码正常工作,没有任何错误。好的,如何正确更改If条件。。是:如果(!$role[0]=='Library'){
?仅将!
感叹号添加到字符串中?您能编辑您的答案吗?谢谢谢伊·阿克谢,非常感谢您更改代码。我正在尝试测试它,但实际上它不起作用。我仍然被重定向为图书管理员和管理员。请检查$role进行打印并检查数组d它是否有我们在条件中设置的确切字符串Hey Akshay,我想你的情况正好相反..我现在被重定向为图书管理员..应该是这样的:如果用户角色不是图书管理员,或者不是管理员,那么重定向到/subscription needed/
,但是如果用户如果是图书管理员
或是管理员
,请停留在当前的图书馆仪表板
页面确定您可以更改我的if条件,我相信您会得到结果。代码正常工作,没有任何错误。确定如何正确更改if条件?。是否为:if(!$role[0]='library'){
?仅将!
感叹号添加到字符串中?您能编辑您的答案吗?谢谢谢伊·阿克谢,非常感谢您更改代码。我正在尝试测试它,但实际上它不起作用。我仍然被重定向为图书管理员和管理员。请检查$role进行打印并检查数组d它有我们在条件中设置的精确字符串吗?你能不能只做如果(数组相交($valid\u roles,$user->roles))
?是的,当然,@markmoxx。但是那时,如果我正确地回忆它,我在原始代码中有var\u dump($the\u roles);
。当我写答案时,我只删除了var\u dump()
并保持其余部分不变。你不能只做如果(array\u intersect($valid\u roles,$user->roles))
?是的,当然,@markmoxx。但是当时,如果我回忆正确,我在原始代码中有var\u dump($the\u roles);
。当我写答案时,我只删除了var\u dump()
,并保持其余部分不变。