edi*_*279 1 wordpress redirect
有没有人知道一种方法(或插件)可以让我根据分配的用户角色自动将用户重定向到另一个页面,如果他们尝试访问主 /wp-admin 页面?
棘手的部分是(我猜),我仍然需要他们能够访问子管理页面(即 mysite.com/wp-admin/edit.php)——如果他们尝试去,它只会重定向它们到主/仪表板页面,mysite.com/wp-admin
小智 5
我最近遇到了需要重定向多个页面的问题。以下代码将检查您要重定向的角色 ($valid-roles),如果无效重定向到给定的 URL...在这种情况下是 /access-denied/。
注意:要重定向的页面数组列表中的页面 ID。
add_action( 'template_redirect', 'role_based_redirect' );
function role_based_redirect() {
if( is_page( array( 1488, 2413, 2379, 2265, 2396, 2370, 2366, 4600 ) ) ) { //check the list of "corporate" pages
$user = wp_get_current_user();
$valid_roles = [ 'administrator', 'corporate', 'editor' ];
$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( '/access-denied/' ) );
exit;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3986 次 |
| 最近记录: |