2014-09-23 128 views
0

我是PHP新手。我试图隐藏某些用户(编辑)的某些仪表板导航项。我已将此添加功能,这隐藏了它的所有用户:使用参数的PHP函数参考

<?php 
function remove_menus(){ 
    remove_menu_page('edit-comments.php');   //Comments 
} 
add_action('admin_menu', 'remove_menus'); 
?> 

它说here你可以使用“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 
)); 
} 

..但是从寻找其他的功能,他们似乎在括号中与=>其间所以我假设我使用这个功能是错误的。

任何帮助,将不胜感激,谢谢。

+0

我认为这是自行编写的代码,因此我正在建议你不要遵循该路线 - 然后我意识到它是Wordpress。对不起,WP是如此的复杂和不合理,它的名字让我很头疼:/ – moonwave99 2014-09-23 10:30:56

+1

仔细看看你链接到的页面上的例子。你应该像这样使用它:'if(current_user_can('edit_pages'))remove_menu_page(...)'。另外看看你可以传递给函数的可能功能列表,我不知道“edit_pages”是否是你正在寻找的功能。 – deceze 2014-09-23 10:34:25

+0

让'remove_menus'更通用 - 比如说,允许许多不同的参数,这些参数允许你删除不同的菜单 - 或者,可以使函数名称更具体,例如, 'remove_edit_comments_menu'? – 2014-09-23 10:42:16

回答

0

第一个答案很简单,使用逻辑“或”运算符:

  <?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 
      } 
     } 
     ?> 

有能力上的更多信息一看here

+0

谢谢!顶级的作品是一种享受。 – RachJenn 2014-09-23 11:17:53