我是PHP新手。我试图对某些用户(编辑器)隐藏某些仪表板导航项目。我已经将其添加到功能中,这为所有用户隐藏了它:
<?php
function remove_menus(){
remove_menu_page( 'edit-comments.php' ); //Comments
}
add_action( 'admin_menu', 'remove_menus' );
?>
这里说你可以使用"current_user_can"来定位某些用户,但我不确定如何将两者结合使用。到目前为止,我已经尝试过:
function remove_menus(){
current_user_can(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
和
function remove_menus(){
current_user_can( array(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
但从其他函数来看,它们似乎在括号中,中间有=>,所以我认为我用错了这个函数。
任何帮助都将不胜感激,谢谢。
第一个答案,非常简单,使用逻辑"OR"运算符:
<?php
function remove_menus(){
if( current_user_can('editor') || current_user_can('administrator') ) { // stuff here for admins or editors
remove_menu_page( 'edit-comments.php' ); //stuff here for editor and administrator
}
} ?>
如果你想检查两个以上的角色,你可以检查当前用户的角色是否在一个角色数组中,比如:
<?php
function remove_menus(){
$user = wp_get_current_user();
$allowed_roles = array('editor', 'administrator', 'author');
if( array_intersect($allowed_roles, $user->roles ) ) {
remove_menu_page( 'edit-comments.php' ); //stuff here for allowed roles
}
} ?>
但是,current_user_can不仅可以与用户角色名称一起使用,还可以与功能一起使用。因此,一旦编辑和管理员都可以编辑页面,您的生活就可以更轻松地检查这些功能:
<?php
function remove_menus(){
if( current_user_can('edit_others_pages') ) {
remove_menu_page( 'edit-comments.php' );// stuff here for user roles that can edit pages: editors and administrators
}
}
?>
查看此处了解有关功能的更多信息。