Deny access for specific admin pages

You do not have sufficient permissions to access this page. – That’s the message you will get if your clientauthor hits a page which is listed in our $page_slugs variable. The real deal for hiding admin menus. Check out this site for other snippets to hide admin pages so they won’t even appear. Also if the users manually enters the admin page url he will get the permission error message.

/**
 * Snippet Name: Deny access for specific admin pages
 * Snippet URL: https://www.wpcustoms.net/snippets/deny-access-for-specific-admin-pages/
 */
  function wpc_disallow_admin_pages(){
    global $pagenow;
    //Skip it for admins
    if(current_user_can('administrator')){
        return;
    }
    // Set denyable & hookable list of admin pages.
    $page_slugs = apply_filters('wpc_disallowed_admin_pages', array(
        'admin.php' => 'jetpack',
        'options-general.php' => ''
    ));
    //Page parameter isn't always present. 
    if(isset($_GET['page'])){
        $page = $_GET['page'];
    } else{
        $page = '';
    }
    // Check current admin page against our denial page_slugs. 
    if(array_key_exists($pagenow, $page_slugs) && in_array($page, $page_slugs)){
        wp_die('You do not have sufficient permissions to access this page.');
    }
}
add_action('admin_init', 'wpc_disallow_admin_pages'); 

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x