public
Last active

How to unset certain admin menu groups in wordpress for specific (named) groups of users.

  • Download Gist
unset-admin-menu.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<?php
/*
Plugin Name:Unset Menu Items
Plugin URI: http://example.com
Description:How to unset certain admin menu groups in wordpress for specific (named) groups of users.
Author: Franz Josef Kaiser
Author URI: http://example.com
Version: 0.1
License: extended MIT/Expat license
 
(c) Copyright 2010-2011 - Franz Josef Kaiser
*/
 
# How to add a user to a user group from inside your functions.php file
function test_me( $user_group_A )
{
$user_group_A[] = 'Take your own name for testing';
 
return $user_group_A;
}
# add_filter( 'user_group_A', 'test_me' );
 
 
function remove_admin_menu_items( $menu )
{
global $menu, $submenu, $current_user;
get_currentuserinfo();
 
$parent = get_admin_page_parent();
 
// don't unset items for admins
#if ( current_user_can( 'manage_options' ) )
#return $menu;
 
// The following menu item groups contain all main menu items
// If you need submenu items, take a look at the foreach loop $item/$data & use the commented <pre>/print_r
 
// Menu Item Groups
// Use the filters to adjust them on demand
$menu_itemset_A = array(
'index.php'
,'edit.php'
,'upload.php'
,'link-manager.php'
,'edit.php?post_type=page'
);
$menu_itemset_A = apply_filters( 'menu_itemset_A', $menu_itemset_A );
 
$menu_itemset_B = array(
'edit-comments.php'
,'themes.php'
,'plugins.php'
,'users.php'
,'tools.php'
,'options-general.php'
);
$menu_itemset_B = apply_filters( 'menu_itemset_B', $menu_itemset_B );
 
// Separators - in case someone wants to remove them too
$separators = array(
'separator1'
,'separator2'
,'separator-last'
);
 
// User Groups
// Use the filters to adjust them on demand
$user_group_A = array(
'User Login Name A'
,'User Login Name B'
,'User Login Name C'
,'...'
);
$user_group_A = apply_filters( 'user_group_A', $user_group_A );
 
$user_group_B = array(
'User Login Name E'
,'User Login Name F'
,'User Login Name G'
,'...'
);
$user_group_B = apply_filters( 'user_group_B', $user_group_B );
 
foreach ( $menu as $item => $data )
{
// Unset Menu Group items A
// Check if user login name is in group A
if ( in_array( $current_user->user_login, $user_group_A ) )
{
#if ( $parent == $menu[$item][2] )
#wp_die( 'You do not have sufficient permissions to access this page.' );
 
if ( in_array( $menu[$item][2], $menu_itemset_A ) )
unset( $menu[$item] );
}
 
// Unset Menu Group items B
// Check if user login name is in group B
elseif ( in_array( $current_user->user_login, $user_group_B ) )
{
#if ( $parent == $menu[$item][2] )
#wp_die( 'You do not have sufficient permissions to access this page.' );
 
if ( $parent == $menu[$item][2] )
unset( $menu[$item] );
}
 
// default behavior
else
{
// do something ... or nothing
}
}
 
// Check if it did what we wanted
# echo '<pre>';
# print_r($menu);
# echo '</pre>';
 
return $menu;
}
add_filter( 'add_menu_classes', 'remove_admin_menu_items' );
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.