SO网友:Mr. Curious
我知道这个问题已经得到了回答,而且已经过时了。然而,我确实想提供另一种解决方案。这就是我在不久前编写的插件中所做的(我修改了代码以使用您的页面)。
因为您想限制参与者角色,所以可以使用role capabilities. 贡献者无法发布帖子,因此您可以执行以下操作。
Part 1: Remove Items from the Admin menu
add_action( \'admin_menu\', \'tcd_remove_admin_menus\' );
function tcd_remove_admin_menus() {
// don\'t do anything if the user can publish posts
if ( current_user_can( \'publish_posts\' ) ) {
return;
}
// remove these items from the admin menu
remove_menu_page( \'edit.php\' ); // Posts
remove_menu_page( \'upload.php\' ); // Media
remove_menu_page( \'tools.php\' ); // Tools
remove_menu_page( \'edit-comments.php\' ); // Comments
}
正如您所说的,它并没有限制用户只需输入直接页面url。以下是我如何编写页面限制:
Part 2: Restrict Access to Admin Pages
add_action( \'current_screen\', \'tcd_restrict_admin_pages\' );
function tcd_restrict_admin_pages() {
// don\'t do anything if the user can publish posts
if ( current_user_can( \'publish_posts\' ) ) {
return;
}
// retrieve the current page\'s ID
$current_screen_id = get_current_screen()->id;
// determine which screens are off limits
$restricted_screens = array(
\'edit\',
\'upload\',
\'tools\',
\'edit-comments\',
);
// Restrict page access
foreach ( $restricted_screens as $restricted_screen ) {
// compare current screen id against each restricted screen
if ( $current_screen_id === $restricted_screen ) {
wp_die( __( \'You are not allowed to access this page.\', \'tcd\' ) );
}
}
}
对我来说,使用角色功能和数组使其更易于使用。无论如何,我希望这个方法是有用的。
干杯