如何在wp中为某些用户隐藏特定页面(从wp-admin)?

时间:2018-10-30 08:03:37

标签: php wordpress

My Image

我只想为某些用户隐藏特定页面。

function remove_menus(){
    // get current login user's role
    $roles = wp_get_current_user()->roles;
    // test role
    if( in_array('administrator',$roles)){
        remove_menu_page( 'edit-comments.php' ); //Posts
        remove_menu_page( 'tools.php' );
        remove_menu_page('edit.php');
        remove_menu_page('wpcf7');
    }

    }
    add_action( 'admin_menu', 'remove_menus' , 100 );

这是我到目前为止尝试过的,并且可以在所有页面上正常工作。

  

我的问题是我不想显示主页-主页(请参阅我的图像)如果登录的用户不是admin。而且我想隐藏添加新

2 个答案:

答案 0 :(得分:1)

您可以使用用户的角色功能,并根据角色允许添加新项目。

function manage_user_action() {

 // get current login user's role
    $roles = wp_get_current_user()->roles;

    if( !in_array('administrator',$roles)){
        //remove capabilities 
        $roles->remove_cap( 'edit_pages');
    }

}
add_action( 'admin_init', 'manage_user_action');

要从列表中删除页面

function jp_exclude_pages_from_admin($query) {

   global $pagenow, $post_type;

  if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
    $query->query_vars['post__not_in'] = array( '10'); // Enter your page IDs here


}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' ); 

有关更多帮助,请参见以下链接:Click Here

答案 1 :(得分:0)

我已经扩展了@dineshkashera答案,以按ID定位特定用户,并且代码应如下所示:

function jp_exclude_pages_from_admin($query) {

       global $pagenow, $post_type;

       $current_user_id = get_current_user_id();

          if ( $current_user_id == 2 && $pagenow == 'edit.php' && $post_type == 'page' )
            $query->query_vars['post__not_in'] = array( '11', '12','13'  ); // Enter your page IDs here

    }
    add_filter( 'parse_query', 'jp_exclude_pages_from_admin' ); 

此代码将获得ID为2的用户,并从其WP页面编辑屏幕中删除页面ID 11,12和13。