最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

removehide pages from users backend

programmeradmin2浏览0评论

I'm using this code in functions.php to hide/remove some pages from custom users backend and works well, but how can I reach to hide the subpages as well?

add_filter( 'parse_query' , 'exclude_pages_from_admin' );

function exclude_pages_from_admin($query) {

global $pagenow,$post_type;
 if (is_admin() && $pagenow == 'edit.php' && $post_type == 'page' && current_user_can( 'custom_role' )) {
    $query->query_vars['post__not_in'] = array('1','2','3');
 }

}

I'm using this code in functions.php to hide/remove some pages from custom users backend and works well, but how can I reach to hide the subpages as well?

add_filter( 'parse_query' , 'exclude_pages_from_admin' );

function exclude_pages_from_admin($query) {

global $pagenow,$post_type;
 if (is_admin() && $pagenow == 'edit.php' && $post_type == 'page' && current_user_can( 'custom_role' )) {
    $query->query_vars['post__not_in'] = array('1','2','3');
 }

}
Share Improve this question asked Jan 25, 2017 at 11:16 mesumesu 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

One way to achieve this is by looping through the parent pages and fetching their respective children pages ids.

The resulting arrays can then be merged and used in the 'post__not_in' variable.

add_filter( 'parse_query' , 'exclude_pages_from_admin' );

function exclude_pages_from_admin( $query ) {

    global $pagenow,$post_type;
    $pages   = array('1','2','3');
    foreach ( $pages as $parent_page ) {
        $args = array(
            'post_type' => 'page',
            'post_parent' => $parent_page,
            'fields' => 'ids',
        );
        $children = new WP_Query( $args );
        $pages = array_merge( $pages, $children );
    }
    if ( is_admin() && $pagenow == 'edit.php' && $post_type == 'page' && current_user_can('custom_role') ) {
        $query->query_vars['post__not_in'] = $pages;
    }

}

Edit: Sorry I think I got the wrong end of the stick on this question! This method is for removing menu page.

I think a simpler method might be:

add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {

    global $user_ID;

    if ( current_user_can( 'author' ) ) {
        remove_menu_page( 'edit.php' ); // Posts
        remove_menu_page('upload.php'); // Media
        remove_menu_page('edit-comments.php'); // Comments
        remove_menu_page('tools.php'); // Tools
        remove_menu_page( 'wpcf7' ); // Contact Form 7
        remove_menu_page('acf-options'); //acf options
    }

    if ( current_user_can( 'editor' ) ) {
        remove_menu_page('upload.php'); // Media
        remove_menu_page('edit-comments.php'); // Comments
        remove_menu_page('tools.php'); // Tools
        remove_menu_page( 'wpcf7' ); // Contact Form 7
        remove_menu_page( 'edit.php?post_type=acf' ); // ACF
        remove_menu_page( 'admin.php?page=cptui_manage_post_types' ); //CPT UI
    }

}

This is straight out of a current project that I am working on and works a treat!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论