• I’m restricting the admin options available to non-admin users and want to remove the ‘categories’ and ‘tags’ options from ‘posts’ and also a custom post type, ‘books’.

    At present, I am restricting what they have access to with the following code:

    add_action( 'admin_init', 'my_remove_menu_pages' );
    add_action('admin_menu', 'remove_menus');
    function remove_menus () {
    global $menu;
    if( (current_user_can('install_themes')) ) { $restricted = array(__()); }
    else { $restricted = array( __('Comments'),__('Links'), __('Pages'), __('Tools'), __('Users'), __('Settings'),__('Profile')); } // hide these for other roles
    end ($menu);
    while (prev($menu)){
    $value = explode(' ',$menu[key($menu)][0]);
    if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
    }
    }

    Can anyone tell me what modifications I would need to make to remove the aforementioned items also? Is there a simple __(‘???’), as used in the above code?

    Thanks

Viewing 1 replies (of 1 total)
  • You can remove top level menus with remove_menu_page(). This function needs the unique menu slug of the menu you wish to remove. You can get a list of top level menus using: var_dump($GLOBALS['menu']);

    Each top level menu is an array within the larger global menu array and contains among other information the capabilities needed to view the menu and also the menu slug needed to identify and remove the menu from view. The array for the Dashboard top level menu looks like this:

    array(7) { [0]=> string(9) "Dashboard" [1]=> string(4) "read" [2]=> string(9) "index.php" [3]=> string(0) "" [4]=> string(43) "menu-top menu-top-first menu-icon-dashboard" [5]=> string(14) "menu-dashboard" [6]=> string(3) "div" }

    The capability needed to view a menu is located at index [1], in this case ‘read’ and the menu slug is found at index [2], in this case ‘index.php’

    Removing submenus is done in a similar fashion using remove_submenu_page(). This function takes the menu slug of the parent as well as a unique submenu slug.

    You can get submenu information with: var_dump($GLOBALS['submenu']);

    Unlike the top level menu array, submenus are indexed using their parents menu slug. For example, the array with index ‘index.php’ (menu slug for the Dashboard) contains two additional arrays which map to two submenus (Home and Updates).

    ["index.php"]=> array(2) { [0]=> array(3) { [0]=> string(4) "Home" [1]=> string(4) "read" [2]=> string(9) "index.php" } [10]=> array(3) { [0]=> string(110) "Updates 1" [1]=> string(11) "update_core" [2]=> string(15) "update-core.php" }

    submenu arrays, like those of top level menus, contain required capabilities information about the submenu and also the submenu slug. For the submenu ‘Home’ required capability = ‘read’ and submenu slug = ‘index.php’. For submenu ‘Updates’, required capability = ‘update_core’ and submenu slug = ‘update-core.php’

    Once you have your menu and submenu slugs, your function can be reworked as follows:

    add_action( 'admin_menu', 'remove_menus' );
    function remove_menus() {
    
        if( false == current_user_can('install_themes') ) {
            // remove top level menus if user cannot install themes
    	$restricted_menus = array( 'edit-comments.php', 'link-manager.php', 'edit.php?post_type=page', 'tools.php', 'users.php', 'options-general.php', 'profile.php' );
    	foreach($restricted_menus as $menu_slug) {
    	    remove_menu_page($menu_slug);
            }
    
            // remove sub-menus if user cannot install themes
    	$restricted_submenus = array(
                'edit-tags.php?taxonomy=category' => 'edit.php', // Post Categories
                'edit-tags.php?taxonomy=post_tag' => 'edit.php', // Post Tags
                );
            foreach($restricted_submenus as $submenu_slug => $menu_slug) {
    	    remove_submenu_page($menu_slug, $submenu_slug);
    	}
        }
    }

    * check the global menu array to get the menu slug for a custom post type e.g. ‘books’ and add that slug to the $restricted_menu array.

    ** you may also run into a problem removing tag or category submenus of a custom post type. For example, a submenu slug can look like ‘edit-tags.php?taxonomy=cpt_tags&post_type=cpt’ in the array but really you need to pass ‘edit-tags.php?taxonomy=cpt_tags&post_type=cpt’ to remove_submenu_page. You can’t see it, but & fromt the first string has been encoded and replaced with & a m p ; in the second.

Viewing 1 replies (of 1 total)
  • The topic ‘Problem removing custom type submenu for certain roles’ is closed to new replies.