如何删除admin以下用户的admin菜单项和Disable访问?正确的方式。。。
// 1 - Remove Menu Items
function my_remove_menu_pages() {
if ( !current_user_can( \'administrator\' ) ) {
remove_menu_page(\'options-general.php\'); // Settings
remove_menu_page(\'tools.php\'); // Tools
remove_menu_page(\'upload.php\'); // Media
remove_menu_page(\'plugins.php\');
remove_menu_page(\'themes.php\');
remove_menu_page(\'edit-comments.php\');
remove_menu_page(\'edit.php?post_type=page\');
remove_menu_page(\'link-manager.php\');
}
}
add_action( \'admin_init\', \'my_remove_menu_pages\' );
// 2 - Redirect users if they visit links from non visible menu items
function no_permission_admin_redirect() {
if ( !current_user_can( \'administrator\' ) ) {
if (stripos($_SERVER[\'REQUEST_URI\'],\'tools.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'options-general.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'upload.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'media.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'edit-comments.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'post-new.php?post_type=page\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'post-new.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'media-new.php\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'edit.php?post_type=page\') !== false
|| stripos($_SERVER[\'REQUEST_URI\'],\'link-manager.php\') !== false) {
wp_redirect(get_option(\'siteurl\') . \'/wp-admin/index.php?permissions_error=true\');
}
}
}
add_action(\'admin_menu\',\'no_permissions_admin_redirect\');
// 3 - Show error message to users if they visit links from non visible menu items
function no_permissions_admin_notice() {
echo "<div id=\'permissions-warning\' class=\'error fade\'><p><strong>".__(\'You do not have permission to access that page.\')."</strong></p></div>";
}
function no_permissions_show_notice() {
if($_GET[\'permissions_error\']) {
add_action(\'admin_notices\', \'no_permissions_admin_notice\');
}
}
add_action(\'admin_init\',\'no_permissions_show_notice\');
这段代码可以工作,但不幸的是,用户可以通过在浏览器上键入url来访问某些页面,而不会出现任何错误或重定向。
如果用户在浏览器上键入url而不使用插件访问页面,是否有一种简单的方法可以删除菜单项并将用户重定向到仪表板?