WordPress Snippets at WPcustoms

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 client\author 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://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');